local skynet = require "skynet"
local socket = require "socket"
local packdeal = require "packdeal"
require "skynet.manager" -- import skynet.register
local ErrNo = require "common/ErrNo"
local NetCmd = require "common/NetCmd"

local json = require "cjson"
local Common = require "common/Common"
local config_redis = require "config_redis"
json.encode_sparse_array(true)

local send_package = packdeal.send_package
local unpack_package = packdeal.unpack_package
uid_score_all = {}
local room_status = 0 --0 游戏还没开始  1 可以下注 2 停止下注

CMD = {}
REQUEST = {}
room_data = {} --系统配置
PLAYR_OTHER = {}
odds_now = { 0, 0, 0, 0, 0, 0, 0, 0 }
odds_uid = {}
score_all = 0
score_old_all = 0
ai_score_all = 0
tourist_score_all = 0
uid_is_admin = {} --该用户是管理员

local card_game_id = 0
local odds_stop_time = 0 --倒计时
local odds_begin_time = 0 --开始时间
local odds_begin_time_switch = 0 --开局计时开关 0关闭1开启
local admin_uid = 2
local ludan_card_game_id = {}
local ludan = {}
local ludan_num_count = { 0, 0, 0, 0, 0, 0, 0 }
local ludan_new = {}
local ludan_sx = {}
user_ai = {}
jiesan_timer_id = nil
odds_stop_time_timer_id = nil
send_odds_table_timer_id = nil
uid_odds_last = 0
uid_odds_last_msg = ''
notice_arr = {}

uid_odds_msg_old = {}

admin_msg_old = {} --当局历史消息
--admin_msg_old_old = {} --上局历史消息

local ludan_da = {} --大路
local ludan_da_new = {} --大路
local ludan_dyz = {} --大眼仔路
local ludan_jy = {} --曱甴路
local ludan_xiao = {} --小路规则
local ludan_yc = {} --路单预测
function send_data(id, data, flag)
    if flag == nil and id == admin_uid and (data.cmd == 3004 or data.cmd == 3006) then
        local a = clone(data)
        a.old = 1
        if a.room_info ~= nil then
            a.room_info = {
                room_id = a.room_info.room_id,
                boots_number = a.room_info.boots_number,
                ju = a.room_info.ju,
            }
        end
        if a.set ~= nil and (a.set == 31 or a.set == 32 or a.set == 33) then

        else
            table.insert(admin_msg_old, a)
        end
    end
    if PLAYR_OTHER[id] ~= nil and PLAYR_OTHER[id].socket ~= nil then
        send_package(PLAYR_OTHER[id].socket, data, id, id)
    end
end

--给所有管理员下发消息
function send_data_to_admin(id, data, flag)
    for k, v in pairs(uid_is_admin) do
        if flag == nil and (data.cmd == 3004 or data.cmd == 3006) then
            local a = clone(data)
            a.old = 1
            if a.room_info ~= nil then
                a.room_info = {
                    room_id = a.room_info.room_id,
                    boots_number = a.room_info.boots_number,
                    ju = a.room_info.ju,
                }
            end
            if a.set ~= nil and (a.set == 31 or a.set == 32 or a.set == 33) then

            else
                table.insert(admin_msg_old, a)
            end
        end

        if PLAYR_OTHER[v] ~= nil and PLAYR_OTHER[v].socket ~= nil then
            send_package(PLAYR_OTHER[v].socket, data, v, v)
        end
    end
end


function send_data_other_pgz(data, pack_send_uid)
    for uid, v in pairs(PLAYR_OTHER) do
        if not pack_send_uid[uid] then
            send_data(uid, data)
        end
    end
end

function send_other(uid, data)
    local pack_send_uid = {}
    pack_send_uid[uid] = 1
    send_data_other_pgz(data, pack_send_uid)
end

function send_all(data)
    send_data_other_pgz(data, {})
end

function win_result(card_game_id_tmp, zhuang_win, zhuang_dui, xian_dui, data, zhuang_total, xian_total, he_total, sb_total, zd_total, xd_total, dc_total, ws, tm, tmbj, tmcm, tmyk, wsyk, sbyk, dcyk, dlzy, zxxm, sbxm, khyk)
    local query_sql = 'begin;' --数据库事务开始
    local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
    local odds_uid_old = clone(odds_uid)
    local re_ukey = card_game_id_tmp --结算唯一标识
    --正常结算
    local type_str = zhuang_win
    for i, v in pairs(odds_uid_old) do
        local win = v[zhuang_win] * get_rate(zhuang_win, 1)
        --开和 庄 闲 投注返回
        if zhuang_win == 3 then
            win = win + v[1] + v[2]
        end

        if zhuang_dui == 4 then
            win = win + v[zhuang_dui] * get_rate(zhuang_dui, 1)
        end

        if xian_dui == 5 then
            win = win + v[xian_dui] * get_rate(xian_dui, 1)
        end
        local uid = i
        if PLAYR_OTHER[uid].ai == 0 then
            --下注合并，修改输赢win
            local query_sql = string.format("UPDATE `bets_merge` SET win=win+%.2f,state=1 WHERE state=0 and card_game_id=%d and uid=%d", win, card_game_id, uid)
            skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        end
        if win > 0 then
            win = math.floor(win * 100) / 100
            if room_status == 2 and card_game_id_tmp == card_game_id then
                odds_uid[i].win = win + odds_uid[i].win
            end

            local db_data = {}
            db_data.uid = uid
            db_data.card_game_id = card_game_id_tmp
            db_data.time = os.date("%Y-%m-%d %H:%M:%S")
            db_data.text = ''
            db_data.type = 111 --牌局结算
            db_data.odds = win
            db_data.score = PLAYR_OTHER[uid].score
            db_data.rate = 1
            db_data.score_change = win
            db_data.score_after = win + PLAYR_OTHER[uid].score
            db_data.user_ai = PLAYR_OTHER[uid].ai
            db_data.tourist = PLAYR_OTHER[uid].tourist
            db_data.orderid = card_game_id_tmp
            db_data.ukey = re_ukey .. '_' .. i .. '_111'
            local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
            if user_score_log_id > 0 then
                local query_sql = string.format("update user set score = score + %f where uid = %d", win, uid)
                skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            end

            PLAYR_OTHER[uid].score = PLAYR_OTHER[uid].score + win
            --总分变化
            if PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 0 then
                score_all = score_all + win
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            elseif PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 1 then
                tourist_score_all = tourist_score_all + win
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            end
            REQUEST.c2s_bjl_user_money(uid)
        end
    end

    if zhuang_dui == 4 then
        type_str = string.format("%s,%s", type_str, zhuang_dui)
    end
    if xian_dui == 5 then
        type_str = string.format("%s,%s", type_str, xian_dui)
    end

    --开和庄闲返回，输赢为0
    if zhuang_win == 3 then
        local query_sql_he = string.format("update `bets_log` set state=1,win=0 where card_game_id = %d and type in (1,2) and state = 0", card_game_id_tmp)
        skynet.call("MG_SAVE", "lua", "update_sql", query_sql_he)
    end
    --修改输的记录
    local query_sql_loser = string.format("update `bets_log` set state=1,win=-odds,xm=odds where card_game_id = %d and type not in (%s) and state = 0", card_game_id_tmp, type_str)
    skynet.call("MG_SAVE", "lua", "update_sql", query_sql_loser)
    --修改赢的记录
    local query_sql = string.format("update `bets_log` set state=1,win=odds*rate where card_game_id = %d and type in (%s) and state = 0", card_game_id_tmp, type_str)
    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if res and res.errno then
        local query_sql = 'rollback;' --数据库事务回退
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        return 0
    end

    if room_status == 2 and card_game_id_tmp == card_game_id then
        local odds_text = '庄' .. odds_now[1]
        local odds_text = odds_text .. '|闲' .. odds_now[2]
        local odds_text = odds_text .. '|和' .. odds_now[3]
        local odds_text = odds_text .. '|庄对' .. odds_now[4]
        local odds_text = odds_text .. '|闲对' .. odds_now[5]
        local odds_text = odds_text .. '|三宝' .. odds_now[6]

        local tmp_s = 0
        odds_now[8] = 0
        odds_now[6] = 0
        if odds_now[1] ~= 0 or odds_now[2] ~= 0 then
            tmp_s = math.abs(odds_now[1] - odds_now[2])
            if room_data.mantissa > 0 then
                odds_now[8] = tmp_s % room_data.mantissa
                odds_now[6] = math.floor(tmp_s / room_data.mantissa) * room_data.mantissa
            elseif room_data.mantissa == 0 then
                odds_now[6] = tmp_s
            end
        end

        local odds_text = odds_text .. '|推码' .. odds_now[6]
        local odds_text = odds_text .. '|对冲' .. tmp_s
        local odds_text = odds_text .. '|尾数' .. odds_now[8]
        data.odds_text = odds_text
        data.odds_now = odds_now
    else
        local odds_now_old = data.odds_now
        local odds_text = '庄' .. odds_now_old[1]
        local odds_text = odds_text .. '|闲' .. odds_now_old[2]
        local odds_text = odds_text .. '|和' .. odds_now_old[3]
        local odds_text = odds_text .. '|庄对' .. odds_now_old[4]
        local odds_text = odds_text .. '|闲对' .. odds_now_old[5]
        local odds_text = odds_text .. '|三宝' .. odds_now_old[6]

        local tmp_s = 0
        odds_now[8] = 0
        odds_now_old[6] = 0
        if odds_now[1] ~= 0 or odds_now[2] ~= 0 then
            tmp_s = math.abs(odds_now[1] - odds_now[2])
            if room_data.mantissa > 0 then
                odds_now[8] = tmp_s % room_data.mantissa
                odds_now_old[6] = math.floor(tmp_s / room_data.mantissa) * room_data.mantissa
            elseif room_data.mantissa == 0 then
                odds_now_old[6] = tmp_s
            end
        end

        local odds_text = odds_text .. '|推码' .. odds_now_old[6]
        local odds_text = odds_text .. '|对冲' .. math.abs(odds_now_old[1] - odds_now_old[2])
        local odds_text = odds_text .. '|尾数' .. odds_now_old[8]
        data.odds_text = odds_text
    end

    local zhuang_dui_db = 0
    local xian_dui_db = 0
    if zhuang_dui > 0 then
        zhuang_dui_db = 1
    end
    if xian_dui > 0 then
        xian_dui_db = 1
    end
    local query_sql = string.format("update `card_game` set zhuang=%d,zhuang_dui=%d,xian_dui=%d,text='%s',zhuang_total=%0.2f,xian_total=%0.2f,zd_total=%0.2f,xd_total=%0.2f,he_total=%0.2f,sb_total=%0.2f,dc_total=%0.2f,ws_total=%0.2f,tm_total=%0.2f,tmbj=%0.2f,tmcm=%0.2f,tmyk=%0.2f,wsyk =%0.2f,sbyk=%0.2f,dcyk=%0.2f,dlzy=%0.2f,zxxm=%0.2f,sbxm=%0.2f,khyk=%0.2f ,state=1 where id = %d", zhuang_win, zhuang_dui_db, xian_dui_db, json.encode(data), zhuang_total, xian_total, zd_total, xd_total, he_total, sb_total, dc_total, ws, tm, tmbj, tmcm, tmyk, wsyk, sbyk, dcyk, dlzy, zxxm, sbxm, khyk, card_game_id_tmp)
    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if res and res.errno then
        local query_sql = 'rollback;' --数据库事务回退
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        return 0
    end
    local query_sql = 'commit;' --数据库事务执行
    local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
    return 1
end


--重新结算
function win_result_recount(card_game_id_tmp, zhuang_win, zhuang_dui, xian_dui, data, zhuang_total, xian_total, he_total, sb_total, zd_total, xd_total, dc_total, ws, tm, tmbj, tmcm, tmyk, wsyk, sbyk, dcyk, dlzy, zxxm, sbxm, khyk)
    local query_sql = 'begin;' --数据库事务开始
    local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
    local card_game_recount = zhuang_win .. '_' .. zhuang_dui .. '_' .. xian_dui
    local re_ukey = card_game_id_tmp --结算唯一标识
    re_ukey = card_game_recount .. '_' .. card_game_id_tmp
    local odds_uid_old = {}
    local query_sql = string.format("select * from bets_log where card_game_id = %d and type in (1,2,3,4,5) and state = 1", card_game_id_tmp)
    local rss_odds_uid = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
    local uid_win = {}
    local bets_log_id_str = {}
    for i, v in pairs(rss_odds_uid) do
        if bets_log_id_str == nil then
            bets_log_id_str[v.uid] = v['id']
        elseif bets_log_id_str ~= nil and bets_log_id_str[v.uid] == nil then
            bets_log_id_str[v.uid] = v['id']
        else
            bets_log_id_str[v.uid] = bets_log_id_str[v.uid] .. ',' .. v['id']
        end
        if odds_uid_old[v.uid] == nil then
            odds_uid_old[v.uid] = { 0, 0, 0, 0, 0 }
        end
        if uid_win[v.uid] == nil then
            uid_win[v.uid] = 0
        end
        if v.odds > 0 then
            odds_uid_old[v.uid][v.type] = odds_uid_old[v.uid][v.type] + v.odds
            --扣回下注金额
            uid_win[v.uid] = uid_win[v.uid] + v.odds
        end
        if v.win ~= 0 then
            uid_win[v.uid] = uid_win[v.uid] + v.win
        end
    end
    for uid, v in pairs(uid_win) do
        local query_sql = string.format("select * from user where uid = %d", uid)
        local user_info = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)
        if user_info ~= nil and user_info.score ~= nil then
            local query_sql = string.format("update user set score = score - %f where uid = %d", v, uid)
            skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            local db_data = {}
            db_data.uid = uid
            db_data.card_game_id = card_game_id_tmp
            db_data.time = os.date("%Y-%m-%d %H:%M:%S")
            db_data.text = ''
            db_data.type = 121 --修改牌局,修改前的输赢回退
            db_data.odds = -v
            db_data.score = user_info.score
            db_data.rate = 1
            db_data.score_change = -v
            db_data.score_after = user_info.score - v
            db_data.user_ai = user_info.ai
            db_data.tourist = user_info.tourist
            db_data.orderid = card_game_id_tmp
            db_data.ukey = bets_log_id_str[uid] .. '_121_' .. card_game_recount
            skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
            PLAYR_OTHER[uid].score = user_info.score - v
            REQUEST.c2s_bjl_user_money(uid)
        end
    end

    local query_sql = string.format("update bets_log set win = 0,state = 0 where card_game_id = %d and type in (1,2,3,4,5) and state = 1", card_game_id_tmp)
    local rss_odds_uid = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

    --正常结算
    local type_str = zhuang_win
    for i, v in pairs(odds_uid_old) do
        local win = v[zhuang_win] * get_rate(zhuang_win, 1)
        --开和 庄 闲 投注返回
        if zhuang_win == 3 then
            win = win + v[1] + v[2]
        end

        if zhuang_dui == 4 then
            win = win + v[zhuang_dui] * get_rate(zhuang_dui, 1)
        end

        if xian_dui == 5 then
            win = win + v[xian_dui] * get_rate(xian_dui, 1)
        end
        local uid = i
        if PLAYR_OTHER[uid].ai == 0 then
            --下注合并，修改输赢win
            local query_sql = string.format("UPDATE `bets_merge` SET win=win+%.2f,state=1 WHERE state=0 and card_game_id=%d and uid=%d", win, card_game_id, uid)
            skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        end
        if win > 0 then
            win = math.floor(win * 100) / 100
            if room_status == 2 and card_game_id_tmp == card_game_id then
                odds_uid[i].win = win + odds_uid[i].win
            end

            local db_data = {}
            db_data.uid = uid
            db_data.card_game_id = card_game_id_tmp
            db_data.time = os.date("%Y-%m-%d %H:%M:%S")
            db_data.text = ''
            db_data.type = 111 --牌局结算
            db_data.odds = win
            db_data.score = PLAYR_OTHER[uid].score
            db_data.rate = 1
            db_data.score_change = win
            db_data.score_after = win + PLAYR_OTHER[uid].score
            db_data.user_ai = PLAYR_OTHER[uid].ai
            db_data.tourist = PLAYR_OTHER[uid].tourist
            db_data.orderid = card_game_id_tmp
            db_data.ukey = re_ukey .. '_' .. i .. '_111'
            local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
            if user_score_log_id > 0 then
                local query_sql = string.format("update user set score = score + %f where uid = %d", win, uid)
                skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            end

            PLAYR_OTHER[uid].score = PLAYR_OTHER[uid].score + win
            --总分变化
            if PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 0 then
                score_all = score_all + win
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            elseif PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 1 then
                tourist_score_all = tourist_score_all + win
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            end
            REQUEST.c2s_bjl_user_money(uid)
        end
    end

    if zhuang_dui == 4 then
        type_str = string.format("%s,%s", type_str, zhuang_dui)
    end
    if xian_dui == 5 then
        type_str = string.format("%s,%s", type_str, xian_dui)
    end

    --开和庄闲返回，输赢为0
    if zhuang_win == 3 then
        local query_sql_he = string.format("update `bets_log` set state=1,win=0 where card_game_id = %d and type in (1,2) and state = 0", card_game_id_tmp)
        skynet.call("MG_SAVE", "lua", "update_sql", query_sql_he)
    end
    --修改输的记录
    local query_sql_loser = string.format("update `bets_log` set state=1,win=-odds,xm=odds where card_game_id = %d and type not in (%s) and state = 0", card_game_id_tmp, type_str)
    skynet.call("MG_SAVE", "lua", "update_sql", query_sql_loser)
    --修改赢的记录
    local query_sql = string.format("update `bets_log` set state=1,win=odds*rate where card_game_id = %d and type in (%s) and state = 0", card_game_id_tmp, type_str)
    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if res and res.errno then
        local query_sql = 'rollback;' --数据库事务回退
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        return 0
    end

    if room_status == 2 and card_game_id_tmp == card_game_id then
        local odds_text = '庄' .. odds_now[1]
        local odds_text = odds_text .. '|闲' .. odds_now[2]
        local odds_text = odds_text .. '|和' .. odds_now[3]
        local odds_text = odds_text .. '|庄对' .. odds_now[4]
        local odds_text = odds_text .. '|闲对' .. odds_now[5]
        local odds_text = odds_text .. '|三宝' .. odds_now[6]

        local tmp_s = 0
        odds_now[8] = 0
        odds_now[6] = 0
        if odds_now[1] ~= 0 or odds_now[2] ~= 0 then
            tmp_s = math.abs(odds_now[1] - odds_now[2])
            if room_data.mantissa > 0 then
                odds_now[8] = tmp_s % room_data.mantissa
                odds_now[6] = math.floor(tmp_s / room_data.mantissa) * room_data.mantissa
            elseif room_data.mantissa == 0 then
                odds_now[6] = tmp_s
            end
        end

        local odds_text = odds_text .. '|推码' .. odds_now[6]
        local odds_text = odds_text .. '|对冲' .. tmp_s
        local odds_text = odds_text .. '|尾数' .. odds_now[8]
        data.odds_text = odds_text
        data.odds_now = odds_now
    else
        local odds_now_old = data.odds_now
        local odds_text = '庄' .. odds_now_old[1]
        local odds_text = odds_text .. '|闲' .. odds_now_old[2]
        local odds_text = odds_text .. '|和' .. odds_now_old[3]
        local odds_text = odds_text .. '|庄对' .. odds_now_old[4]
        local odds_text = odds_text .. '|闲对' .. odds_now_old[5]
        local odds_text = odds_text .. '|三宝' .. odds_now_old[6]

        local tmp_s = 0
        odds_now[8] = 0
        odds_now_old[6] = 0
        if odds_now[1] ~= 0 or odds_now[2] ~= 0 then
            tmp_s = math.abs(odds_now[1] - odds_now[2])
            if room_data.mantissa > 0 then
                odds_now[8] = tmp_s % room_data.mantissa
                odds_now_old[6] = math.floor(tmp_s / room_data.mantissa) * room_data.mantissa
            elseif room_data.mantissa == 0 then
                odds_now_old[6] = tmp_s
            end
        end

        local odds_text = odds_text .. '|推码' .. odds_now_old[6]
        local odds_text = odds_text .. '|对冲' .. math.abs(odds_now_old[1] - odds_now_old[2])
        local odds_text = odds_text .. '|尾数' .. odds_now_old[8]
        data.odds_text = odds_text
    end

    local zhuang_dui_db = 0
    local xian_dui_db = 0
    if zhuang_dui > 0 then
        zhuang_dui_db = 1
    end
    if xian_dui > 0 then
        xian_dui_db = 1
    end
    local query_sql = string.format("update `card_game` set zhuang=%d,zhuang_dui=%d,xian_dui=%d,text='%s',zhuang_total=%0.2f,xian_total=%0.2f,zd_total=%0.2f,xd_total=%0.2f,he_total=%0.2f,sb_total=%0.2f,dc_total=%0.2f,ws_total=%0.2f,tm_total=%0.2f,tmbj=%0.2f,tmcm=%0.2f,tmyk=%0.2f,wsyk =%0.2f,sbyk=%0.2f,dcyk=%0.2f,dlzy=%0.2f,zxxm=%0.2f,sbxm=%0.2f,khyk=%0.2f ,state=1 where id = %d", zhuang_win, zhuang_dui_db, xian_dui_db, json.encode(data), zhuang_total, xian_total, zd_total, xd_total, he_total, sb_total, dc_total, ws, tm, tmbj, tmcm, tmyk, wsyk, sbyk, dcyk, dlzy, zxxm, sbxm, khyk, card_game_id_tmp)
    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if res and res.errno then
        local query_sql = 'rollback;' --数据库事务回退
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        return 0
    end
    local query_sql = 'commit;' --数据库事务执行
    local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
    return 1
end

--开启机器人下注
function ai_start()
    if room_data.ai_state == 1 then
        local query_sql = string.format("select * from user where  ai=1 and deleted=0 and uid !=" .. admin_uid)
        local rs = skynet.call("MG_SAVE", "lua", "get_user", 0, query_sql)

        local user_arr = {}
        for i, v in pairs(rs) do
            local uid = tonumber(v.uid)
            if PLAYR_OTHER[uid] == nil then
                PLAYR_OTHER[uid] = {
                    uid = uid,
                    name = v.name,
                    head = v.head,
                    phone_num = v.phone_num,
                    money = v.money,
                    score = v.score,
                    score_old = v.score,
                    ai = 1,
                    no_say = v.no_say,
                    tourist = 0,
                    odds_text = v.odds_text,
                    auto_fen = v.auto_fen,
                    upfen_text = v.upfen_text
                }
            end
            table.insert(user_arr, v)
        end
        if #user_arr > room_data.ai_num then
            local nummmm = #user_arr - room_data.ai_num
            for i = 1, nummmm do
                local kkkk = math.random(1, #user_arr)
                table.remove(user_arr, kkkk)
            end
        end
        user_ai = {}
        for i, v in pairs(user_arr) do
            table.insert(user_ai, v)
        end
        if jiesan_timer_id then
            del_skynet_timer(jiesan_timer_id)
            jiesan_timer_id = nil
        end
        local timeeee = math.random(room_data.ai_time_min, room_data.ai_time_max)
        jiesan_timer_id = add_skynet_timer(timeeee, ai_odds)
    end
end

--机器人下注
function ai_odds()

    if #user_ai > 0 and room_status == 1 then
        local user_ai_now = {}
        local text_ai_now = {}
        local kkk = math.random(1, #user_ai)
        if user_ai[kkk] ~= nil then
            user_ai_now = user_ai[kkk]
            table.remove(user_ai, kkk)
            if PLAYR_OTHER[user_ai_now.uid] == nil then
                PLAYR_OTHER[user_ai_now.uid] = {
                    uid = user_ai_now.uid,
                    name = user_ai_now.name,
                    head = user_ai_now.head,
                    phone_num = user_ai_now.phone_num,
                    money = user_ai_now.money,
                    score = user_ai_now.score,
                    score_old = user_ai_now.score_old,
                    tourist = 0,
                    ai = 1,
                    odds_text = user_ai_now.odds_text,
                    auto_fen = user_ai_now.auto_fen,
                    upfen_text = user_ai_now.upfen_text
                }
            end
        end
        local ai_odds_text = room_data.ai_text
        if user_ai_now.odds_text ~= nil and #user_ai_now.odds_text > 0 then
            ai_odds_text = user_ai_now.odds_text
        end
        local odds_text = split(ai_odds_text, "\r\n")
        if odds_text[1] ~= nil then
            local odds_text2 = split(odds_text[1], ',')
            local kkk = math.random(1, #odds_text2)
            if odds_text2[kkk] ~= nil then
                text_ai_now = odds_text2[kkk]
            end
        end
        text_ai_now = string.gsub(text_ai_now, '=', ',')
        local ai_odds_fen_all = 0
        local odds_arr = split(text_ai_now, ',')
        local is_suoha_zhuang = 0
        local is_suoha_xian = 0
        local odds_arr_new_tmp = { 0, 0, 0, 0, 0 } --用户下注数组
        for i, v in pairs(odds_arr) do
            start = 3
            local msg_str = v
            odds = string.sub(msg_str, start, string.len(msg_str))
            odds = tonumber(odds)
            if odds ~= nil and odds > 0 then
                odds = math.floor(math.abs(odds))
            else
                odds = 0
            end
            if string.sub(msg_str, 1, 2) == 'zd' or string.sub(msg_str, 1, 2) == 'ZD' then
                odds_arr_new_tmp[4] = odds_arr_new_tmp[4] + odds
            elseif string.sub(msg_str, 1, 2) == 'xd' or string.sub(msg_str, 1, 2) == 'XD' then
                odds_arr_new_tmp[5] = odds_arr_new_tmp[5] + odds
            elseif string.sub(msg_str, 1, 2) == 'sb' or string.sub(msg_str, 1, 2) == 'SB' then
                odds_arr_new_tmp[3] = odds_arr_new_tmp[3] + odds
                odds_arr_new_tmp[4] = odds_arr_new_tmp[4] + odds
                odds_arr_new_tmp[5] = odds_arr_new_tmp[5] + odds
            elseif string.sub(msg_str, 1, 2) == 'zs' or string.sub(msg_str, 1, 2) == 'ZS' and is_suoha_zhuang == 0 then
                odds_arr_new_tmp = { 0, 0, 0, 0, 0, 0 }
                odds = PLAYR_OTHER[uid].score
                is_suoha_zhuang = 1
                odds_arr_new_tmp[1] = odds
                break
            elseif string.sub(msg_str, 1, 2) == 'xs' or string.sub(msg_str, 1, 2) == 'XS' and is_suoha_xian == 0 then
                odds_arr_new_tmp = { 0, 0, 0, 0, 0, 0 }
                odds = PLAYR_OTHER[uid].score
                is_suoha_xian = 1
                odds_arr_new_tmp[2] = odds
                break
            else
                start = 2
                odds = string.sub(msg_str, start, string.len(msg_str))
                odds = tonumber(odds)
                if odds ~= nil and odds > 0 then
                    odds = math.floor(math.abs(odds))
                else
                    odds = 0
                end
                if string.sub(msg_str, 1, 1) == 'z' or string.sub(msg_str, 1, 1) == 'Z' then
                    if is_suoha_zhuang == 0 then
                        odds_arr_new_tmp[1] = odds_arr_new_tmp[1] + odds
                    end
                elseif string.sub(msg_str, 1, 1) == 'x' or string.sub(msg_str, 1, 1) == 'X' then
                    if is_suoha_xian == 0 then
                        odds_arr_new_tmp[2] = odds_arr_new_tmp[2] + odds
                    end
                elseif string.sub(msg_str, 1, 1) == 'h' or string.sub(msg_str, 1, 1) == 'H' then
                    odds_arr_new_tmp[3] = odds_arr_new_tmp[3] + odds
                elseif string.sub(msg_str, 1, 1) == 'd' or string.sub(msg_str, 1, 1) == 'D' then
                    odds_arr_new_tmp[4] = odds_arr_new_tmp[4] + odds
                    odds_arr_new_tmp[5] = odds_arr_new_tmp[5] + odds
                end
            end
        end
        for i, v in pairs(odds_arr_new_tmp) do
            ai_odds_fen_all = ai_odds_fen_all + v
        end

        if user_ai_now.score > ai_odds_fen_all then
            REQUEST.odds(user_ai_now.uid, { odds_msg = text_ai_now })
        else
            local query_sql = string.format("select * from user_money_log where  type = 11 and state = 0 and uid = " .. user_ai_now.uid)
            local rs = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            if rs ~= nil and rs[1] == nil then
                if room_data.ai_upfen ~= nil then
                    local ai_upfen_str = room_data.ai_upfen
                    if user_ai_now.upfen_text ~= nil and #user_ai_now.upfen_text > 0 then
                        ai_upfen_str = user_ai_now.upfen_text
                    end
                    local ai_upfen = split(ai_upfen_str, ',')
                    local ai_upfen_key = math.random(1, #ai_upfen)
                    REQUEST.c2s_bjl_upfen(user_ai_now.uid, { fen = ai_upfen[ai_upfen_key], channel = 1 })
                end
            end
        end

        if jiesan_timer_id then
            del_skynet_timer(jiesan_timer_id)
            jiesan_timer_id = nil
        end
        local timeeee = math.random(room_data.ai_time_min, room_data.ai_time_max)
        jiesan_timer_id = add_skynet_timer(timeeee, ai_odds)
    end
end

--倒计时20秒
function odds_stop_20()
    REQUEST.set(admin_uid, { set = 3 })
end

--发送投注表
function send_odds_table()
    local odds_stop_time_new = 0
    if odds_stop_time > 0 then
        odds_stop_time_new = odds_stop_time - os.time()
    end
    local odds_begin_time_cur = 0
    if odds_begin_time_switch == 1 then
        odds_begin_time_cur = os.time() - odds_begin_time
    end
    local web_domain = skynet.call('MG_SAVE', 'lua', 'get_web_domain');
    local packet = {
        cmd = NetCmd.S2C_BJL_SET,
        room_status = room_status,
        set = 3,
        msg = '',
        name = PLAYR_OTHER[admin_uid].name,
        head = PLAYR_OTHER[admin_uid].head,
        time = os.date("%Y-%m-%d %H:%M:%S"),
        color = 2,
        room_id = room_data.room_id,
        boots_number = room_data.boots_number,
        ju = room_data.ju,
        room_info = {
            room_id = room_data.room_id,
            boots_number = room_data.boots_number,
            ju = room_data.ju,
            ludan_zz = ludan_new, --珠子路
            ludan_sx = ludan_sx, --三星路
            ludan_da = ludan_da_new, --大路
            ludan_dyz = ludan_dyz, --大眼仔路
            ludan_jy = ludan_jy, --曱甴路
            ludan_xiao = ludan_xiao, --小路规则
            ludan_yc = ludan_yc, --路单预测
            ludan_data = { ju = ludan_num_count[6], z = ludan_num_count[1], x = ludan_num_count[2], h = ludan_num_count[3], zd = ludan_num_count[4], xd = ludan_num_count[5] }, --局数13 庄5 闲5 和3 庄对3 闲对3
            odds_stop_time = odds_stop_time_new,
            odds_begin_time = odds_begin_time_cur
        },
        cur_time = os.date("%m-%d %H:%M:%S"),
        fabiao = 1
    }
    -- 定时器触发
    odds_now, odds_uid = get_bets_log() --查询下注记录
    local mantissa = math.floor(room_data.mantissa) --尾数吃码向下取整
    local tm = 0
    if odds_now[1] ~= 0 or odds_now[2] ~= 0 then
        if mantissa > 0 then
            tm = (math.floor(math.abs(odds_now[1] - odds_now[2]) / mantissa)) * mantissa --推码
        elseif mantissa == 0 then
            tm = math.abs(odds_now[1] - odds_now[2]) --推码
        end
    end
    if tm < room_data.odds_zx_min then
        tm = 0
    end
    packet.ret = 0
    packet.msg = ''
    packet.tm = tm --推码
    packet.zhuang = odds_now[1] --庄注
    packet.xian = odds_now[2] --闲注
    local data = {}
    local key = 0
    local count = { 0, 0, 0, 0, 0, 0 }
    for i, v in pairs(odds_uid) do
        key = key + 1
        local arr = clone(v)
        if odds_uid[i].score_old ~= nil then
            arr.score_old = odds_uid[i].score_old
        else
            arr.score_old = PLAYR_OTHER[i].score - odds_uid[i].win
        end

        arr.key = key
        arr.name = PLAYR_OTHER[i].name
        if string.len(PLAYR_OTHER[i].name) > 20 then
            arr.name = SubStringUTF8(PLAYR_OTHER[i].name, 1, 20) .. '..'
        end
        arr.z = arr[1]
        arr.x = arr[2]
        arr.h = arr[3]
        arr.zd = arr[4]
        arr.xd = arr[5]
        --排除没下注用户
        if arr[1] + arr[2] + arr[3] + arr[4] + arr[5] > 0 then
            table.insert(data, arr)
        end
        for ii = 1, 6 do
            count[ii] = count[ii] + v[ii]
        end
    end
    count.key = -1
    count.z = count[1]
    count.x = count[2]
    count.h = count[3]
    count.zd = count[4]
    count.xd = count[5]
    table.insert(data, count)
    packet.data = data
    packet.table_name = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局 投注表'
    send_all(packet)
    if send_odds_table_timer_id then
        del_skynet_timer(send_odds_table_timer_id)
        send_odds_table_timer_id = nil
    end
    return
end


function REQUEST.set(uid, data)
    local odds_stop_time_new = 0
    if odds_stop_time > 0 then
        odds_stop_time_new = odds_stop_time - os.time()
    end
    local odds_begin_time_cur = 0
    if odds_begin_time_switch == 1 then
        odds_begin_time_cur = os.time() - odds_begin_time
    end
    local web_domain = skynet.call('MG_SAVE', 'lua', 'get_web_domain');
    local packet = {
        cmd = NetCmd.S2C_BJL_SET,
        room_status = room_status,
        set = data.set,
        msg = '',
        name = PLAYR_OTHER[admin_uid].name,
        head = PLAYR_OTHER[admin_uid].head,
        time = os.date("%Y-%m-%d %H:%M:%S"),
        color = 2,
        room_id = room_data.room_id,
        boots_number = room_data.boots_number,
        ju = room_data.ju,
        room_info = {
            room_id = room_data.room_id,
            boots_number = room_data.boots_number,
            ju = room_data.ju,
            ludan_zz = ludan_new, --珠子路
            ludan_sx = ludan_sx, --三星路
            ludan_da = ludan_da_new, --大路
            ludan_dyz = ludan_dyz, --大眼仔路
            ludan_jy = ludan_jy, --曱甴路
            ludan_xiao = ludan_xiao, --小路规则
            ludan_yc = ludan_yc, --路单预测
            ludan_data = { ju = ludan_num_count[6], z = ludan_num_count[1], x = ludan_num_count[2], h = ludan_num_count[3], zd = ludan_num_count[4], xd = ludan_num_count[5] }, --局数13 庄5 闲5 和3 庄对3 闲对3
            odds_stop_time = odds_stop_time_new,
            odds_begin_time = odds_begin_time_cur
        },
        cur_time = os.date("%m-%d %H:%M:%S"),
    }

    --开局
    if data.set == 1 and room_status == 0 then
        --解锁下注
        local room_status_stop_key = skynet.getenv("port") .. '_room_status_stop'
        local redis_result = skynet.call('MG_SAVE', 'lua', 'redis_del', room_status_stop_key)

        CMD.start()
        if data.room_id ~= nil and data.boots_number ~= nil and data.ju ~= nil then

            local room_id = data.room_id
            local boots_number = data.boots_number
            local ju = data.ju
            local ukey = boots_number .. '_' .. ju .. '_' .. os.date("%Y%m%d%H%M")
            room_data.room_id = room_id
            packet.room_id = room_id
            room_data.boots_number = boots_number
            packet.boots_number = boots_number
            room_data.ju = ju
            packet.ju = ju
            local query_sql = string.format("SELECT id FROM `card_game` WHERE ukey='%s'", ukey)
            local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            if res ~= nil and res[1] ~= nil and res[1]['id'] ~= nil then
                card_game_id = res[1]['id']
            else
                local db_data = {}
                db_data.uid = uid
                db_data.time = os.date("%Y-%m-%d %H:%M:%S")
                db_data.room_id = room_id
                db_data.boots_number = boots_number
                db_data.ju = ju
                db_data.ukey = ukey
                card_game_id = skynet.call("MG_SAVE", "lua", "inser_card_game", db_data)
            end
            if card_game_id == 0 then
                return
            end
        else
            return
        end
        packet.msg = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局  请开始下注'
        room_status = 1
        odds_stop_time = 0
        odds_begin_time = os.time()
        odds_begin_time_switch = 1
        packet.room_status = room_status
        packet.card_game_id = card_game_id
        packet.ret = 0
        send_all(packet)
        --odds_uid初始化
        odds_uid = {}
        uid_odds_msg_old = {}
        admin_msg_old = {}
        odds_now = { 0, 0, 0, 0, 0, 0, 0, 0 }
        REQUEST.set(admin_uid, { set = 31 }) --推送用户列表
        REQUEST.set(admin_uid, { set = 32 }) --推送当局报表
        uid_odds_last = 0
        uid_odds_last_msg = ''

        --        for uid, v in pairs(PLAYR_OTHER) do
        --            if PLAYR_OTHER[uid].ai == 0 then
        --                CMD.packet(uid)
        --            end
        --        end

        ai_start()
        return
    end

    --下注最后20秒
    if data.set == 2 and room_status == 1 then
        packet.msg = '下注最后20秒'
        odds_stop_time = os.time() + 20
        packet.odds_stop_time = 20
        packet.ret = 0
        send_all(packet)
        if data.second == 20 then
            local packet = {
                cmd = NetCmd.S2C_BJL_ODDS,
                uid = admin_uid,
                msg = '下注最后20秒',
                admin_msg = 1, --控制台对话框不显示
                ret = 0,
                name = PLAYR_OTHER[admin_uid].name,
                head = PLAYR_OTHER[admin_uid].head,
                time = os.date("%Y-%m-%d %H:%M:%S"),
                color = 2,
                cur_time = os.date("%m-%d %H:%M:%S"),
            }
            send_all(packet)
        end
        odds_stop_time_timer_id = add_skynet_timer(22, odds_stop_20)
        return
    end

    --停止下注
    if data.set == 3 and room_status == 1 then
        local room_status_stop_key = skynet.getenv("port") .. '_room_status_stop'
        local redis_result = skynet.call('MG_SAVE', 'lua', 'redis_setnx', room_status_stop_key, 1, 10)
        if redis_result == nil then
            skynet.error('已停止下注，请勿重复操作')
            return
        end
        room_status = 2
        if jiesan_timer_id then
            del_skynet_timer(jiesan_timer_id)
            jiesan_timer_id = nil
        end
        if odds_stop_time_timer_id then
            del_skynet_timer(odds_stop_time_timer_id)
            odds_stop_time_timer_id = nil
        end
        odds_begin_time = os.time()
        odds_begin_time_switch = 0
        packet.msg = '停止下注,线下无效!'
        packet.room_status = room_status
        packet.ret = 0
        send_all(packet)
        if send_odds_table_timer_id == nil then
            send_odds_table_timer_id = add_skynet_timer(2, send_odds_table)
        end
    end

    --结算
    if data.set == 4 and room_status == 2 then
        if jiesan_timer_id then
            del_skynet_timer(jiesan_timer_id)
            jiesan_timer_id = nil
        end
        if send_odds_table_timer_id then
            del_skynet_timer(send_odds_table_timer_id)
            send_odds_table_timer_id = nil
        end
        local zhuang_dui = 0
        local xian_dui = 0
        local zhuang_win = 0
        local win_msg = ''
        local zhuang_dian
        local xian_dian
        --牌点开奖
        if data.type == 1 then
            zhuang_dian = math.floor(data.z)
            xian_dian = math.floor(data.x)
            win_msg = '庄赢'
            zhuang_win = 1
            if zhuang_dian < xian_dian then
                zhuang_win = 2
                win_msg = '闲赢'
            elseif zhuang_dian == xian_dian then
                zhuang_win = 3
                win_msg = '和局'
            end
            packet.color = zhuang_win + 2
            if data.p3 % 13 == data.p4 % 13 then
                zhuang_dui = 4
                win_msg = win_msg .. '庄对'
            end
            if data.p1 % 13 == data.p2 % 13 then
                xian_dui = 5
                win_msg = win_msg .. '闲对'
            end
            if zhuang_dui == 0 and xian_dui == 0 then
                win_msg = win_msg .. '无对'
            end
        end
        --直接结果开奖
        if data.type == 2 then
            local zhuang_xian_he = math.floor(data.y)
            zhuang_dui = math.floor(data.zd)
            xian_dui = math.floor(data.xd)
            zhuang_win = zhuang_xian_he
            if zhuang_xian_he == 1 then
                win_msg = '庄赢'
            elseif zhuang_xian_he == 2 then
                win_msg = '闲赢'
            elseif zhuang_xian_he == 3 then
                win_msg = '和局'
            end

            packet.color = zhuang_win + 2
            if zhuang_dui == 4 then
                win_msg = win_msg .. '庄对'
            end
            if xian_dui == 5 then
                win_msg = win_msg .. '闲对'
            end
            if zhuang_dui == 0 and xian_dui == 0 then
                win_msg = win_msg .. '无对'
            end
        end
        local zhuang_dui_db = 0
        local xian_dui_db = 0
        if zhuang_dui > 0 then
            zhuang_dui_db = 1
        end
        if xian_dui > 0 then
            xian_dui_db = 1
        end
        if zhuang_win > 0 then
            local arr = {}
            arr.y = zhuang_win
            arr.zd = zhuang_dui_db
            arr.xd = xian_dui_db
            if ludan_card_game_id[card_game_id] == nil then
                ludan_card_game_id[card_game_id] = true
                table.insert(ludan, arr)
            end
            ludan_da_update()
        end

        for uid, _ in pairs(PLAYR_OTHER) do
            if uid ~= admin_uid then
                send_user_info(uid, PLAYR_OTHER[uid], 1)
            end
        end

        local tmbj = room_data.capital --台面本金
        local tmcm = 0 --台面筹码
        local tmyk = 0 --台面盈亏
        local wsyk = 0 --尾数盈亏
        local sbyk = 0 --三宝盈亏
        local dcyk = 0 --对冲盈亏
        local dlzy = 0 --代理总赢
        local zxxm = 0 --庄闲洗码
        local sbxm = 0 --三宝洗码
        local khyk = 0 --客户盈亏
        uid_odds_msg_old = {}
        local bets_odds_now, bets_odds_uid = get_bets_log(0, zhuang_win, zhuang_dui, xian_dui) --查询下注记录
        odds_uid = bets_odds_uid
        local zhuang_total = bets_odds_now[1] --庄的下注总额
        local xian_total = bets_odds_now[2] --闲的下注总额
        local dc_total = math.min(bets_odds_now[1], bets_odds_now[2]) --对冲的金额
        local mantissa = math.floor(room_data.mantissa) --尾数吃码向下取整
        local tm = 0
        local ws = 0
        if bets_odds_now[1] ~= 0 or bets_odds_now[2] ~= 0 then
            if mantissa > 0 then
                tm = (math.floor(math.abs(bets_odds_now[1] - bets_odds_now[2]) / mantissa)) * mantissa --推码
            elseif mantissa == 0 then
                tm = math.abs(bets_odds_now[1] - bets_odds_now[2]) --推码
            end
            if tm < room_data.odds_zx_min then
                tm = 0
            end
            ws = math.abs(bets_odds_now[1] - bets_odds_now[2]) - tm --尾数
        end

        --对冲盈亏
        if zhuang_win == 1 then
            dcyk = dcyk + dc_total - (dc_total * room_data.zhuang)
        end

        --统计尾数盈亏，庄闲洗码
        if zhuang_total > xian_total then
            --庄的总额大于闲，尾数，推码下庄
            if zhuang_win == 1 then
                wsyk = -(ws * room_data.zhuang)
                tmyk = -(tm * room_data.zhuang)
            elseif zhuang_win == 2 then
                wsyk = ws
                zxxm = tm
                tmyk = tm
            end
        elseif zhuang_total < xian_total then
            --庄的总额小于闲，尾数，推码下闲
            if zhuang_win == 1 then
                wsyk = ws
                zxxm = tm
                tmyk = tm
            elseif zhuang_win == 2 then
                wsyk = -(ws * room_data.xian)
                tmyk = -(tm * room_data.xian)
            end
        end

        --统计三宝洗码，三宝盈亏
        local zd_total = 0 --庄对投注总额
        local xd_total = 0 --闲对投注总额
        local he_total = 0 --和投注总额
        if bets_odds_now[4] ~= nil and bets_odds_now[4] > 0 then
            zd_total = bets_odds_now[4]
        end
        if bets_odds_now[5] ~= nil and bets_odds_now[5] > 0 then
            xd_total = bets_odds_now[5]
        end
        if bets_odds_now[3] ~= nil and bets_odds_now[3] > 0 then
            he_total = bets_odds_now[3]
        end
        local sb_total = zd_total + xd_total + he_total --三宝总额

        if zhuang_win == 3 then
            sbyk = sbyk - he_total * room_data.he
        else
            sbyk = sbyk + he_total
            sbxm = sbxm + he_total
        end

        if zhuang_dui == 4 then
            sbyk = sbyk - zd_total * room_data.zhuang_dui
        else
            sbyk = sbyk + zd_total
            sbxm = sbxm + zd_total
        end

        if xian_dui == 5 then
            sbyk = sbyk - xd_total * room_data.xian_dui
        else
            sbyk = sbyk + xd_total
            sbxm = sbxm + xd_total
        end

        --统计代理总赢
        dlzy = wsyk + sbyk + dcyk
        --客户盈亏
        khyk = -(wsyk + sbyk + dcyk + tmyk)
        --台面筹码
        tmcm = tmbj + dlzy + khyk

        local rs = win_result(card_game_id, zhuang_win, zhuang_dui, xian_dui, data, zhuang_total, xian_total, he_total, sb_total, zd_total, xd_total, dc_total, ws, tm, tmbj, tmcm, tmyk, wsyk, sbyk, dcyk, dlzy, zxxm, sbxm, khyk)
        if rs == 0 then
            packet.msg = '程序异常,请重新操作'
            packet.ret = 2
            send_data(uid, packet)
            return
        end
        if zhuang_dian ~= nil and xian_dian ~= nil then
            packet.msg = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局  庄家:' .. zhuang_dian .. '点 闲家:' .. xian_dian .. '点' .. win_msg
            packet.msg_win = {}
            packet.msg_win[1] = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局'
            packet.msg_win[2] = '庄家:' .. zhuang_dian .. '点'
            packet.msg_win[3] = '闲家:' .. xian_dian .. '点'
            packet.msg_win[4] = win_msg
            packet.msg_admin = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局<br>  庄家:' .. zhuang_dian .. '点<br> 闲家:' .. xian_dian .. '点<br>' .. win_msg
        else
            packet.msg = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局' .. win_msg
            packet.msg_win = {}
            packet.msg_win[1] = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局'
            packet.msg_win[2] = ''
            packet.msg_win[3] = ''
            packet.msg_win[4] = win_msg
            packet.msg_admin = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局<br>' .. win_msg
        end
        room_status = 0
        odds_stop_time = 0
        packet.room_status = room_status
        local data_arr = {}
        local key = 0
        local count = { win = 0, score = 0, score_old = 0 }
        local userHasScores = skynet.call("MG_SAVE", "lua", "getUserHasScore")
        --所有有余额的人
        for i, v in pairs(userHasScores) do
            local userid = userHasScores[i]['uid']
            if PLAYR_OTHER[userid] == nil then
                PLAYR_OTHER[userid] = {
                    uid = userid,
                    name = userHasScores[i]['name'],
                    head = userHasScores[i]['head'],
                    phone_num = userHasScores[i]['phone_num'],
                    money = userHasScores[i]['money'],
                    score = userHasScores[i]['score'],
                    score_old = userHasScores[i]['score'],
                    no_say = userHasScores[i]['no_say'],
                    ai = userHasScores[i]['ai'],
                    tourist = userHasScores[i]['tourist']
                }
            end
            if odds_uid[userid] == nil then
                odds_uid[userid] = { 0, 0, 0, 0, 0, 0, score_old = PLAYR_OTHER[userid].score_old, score = PLAYR_OTHER[userid].score, ai = userHasScores[i].ai, tourist = userHasScores[i].tourist, win = 0 }
            end
        end
        for i, v in pairs(odds_uid) do
            if i ~= admin_uid then
                key = key + 1
                local arr = {}
                arr.key = key
                arr.name = PLAYR_OTHER[i].name
                if string.len(PLAYR_OTHER[i].name) > 20 then
                    arr.name = SubStringUTF8(PLAYR_OTHER[i].name, 1, 20) .. '..'
                end
                arr.score = PLAYR_OTHER[i].score
                if PLAYR_OTHER[i].score ~= nil and PLAYR_OTHER[i].score < 0 and PLAYR_OTHER[i].ai == 1 then
                    arr.score = 0
                    PLAYR_OTHER[i].score = 0
                end

                arr.win = odds_uid[i].win
                if odds_uid[i].score_old ~= nil then
                    arr.score_old = odds_uid[i].score_old
                else
                    arr.score_old = PLAYR_OTHER[i].score - odds_uid[i].win
                end
                arr.score = math.floor(arr.score)
                arr.score_old = math.floor(arr.score_old)
                --排除没下注用户
                --if v[1] + v[2] + v[4] + v[4] + v[5] > 0 then
                if PLAYR_OTHER[i].score > 1 or v[1] + v[2] + v[3] + v[4] + v[5] > 0 then
                    count.win = count.win + odds_uid[i].win
                    count.score = count.score + arr.score
                    if arr.score_old ~= nil then
                        count.score_old = count.score_old + arr.score_old
                    end
                    table.insert(data_arr, arr)
                end
                --end
            end
        end
        table.sort(data_arr, function(a, b)
            return a.score > b.score
        end)
        --重新排序号
        key = 0
        for i, v in pairs(data_arr) do
            key = key + 1
            data_arr[i]['key'] = key
        end
        count.key = -1
        table.insert(data_arr, count)
        packet.data = data_arr
        packet.p1 = data.p1
        packet.p2 = data.p2
        packet.p3 = data.p3
        packet.p4 = data.p4
        packet.p5 = data.p5
        packet.p6 = data.p6
        packet.table_name = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局 余额表'
        room_data.ju = room_data.ju + 1
        packet.ju = room_data.ju
        packet.ret = 0

        packet.room_info.ludan_zz = ludan_new --珠子路
        packet.room_info.ludan_sx = ludan_sx --三星路
        packet.room_info.ludan_da = ludan_da_new --大路
        packet.room_info.ludan_dyz = ludan_dyz --大眼仔路
        packet.room_info.ludan_jy = ludan_jy --曱甴路
        packet.room_info.ludan_xiao = ludan_xiao --小路规则
        packet.room_info.ludan_yc = ludan_yc --路单预测
        packet.room_info.ludan_data = {
            ju = ludan_num_count[6],
            z = ludan_num_count[1],
            x = ludan_num_count[2],
            h = ludan_num_count[3],
            zd = ludan_num_count[4],
            xd = ludan_num_count[5]
        }
        send_all(packet)

        local data_tongji_zy = {}
        local zy_data = skynet.call("MG_SAVE", "lua", "get_zy_tongji")
        local wsyk_sum = 0
        local sbyk_sum = 0
        local dcyk_sum = 0
        local zxxm_sum = 0
        local sbxm_sum = 0
        local tmyk_sum = 0
        if zy_data ~= nil and zy_data[1] ~= nil and zy_data[1]['wsyk'] ~= nil then
            wsyk_sum = zy_data[1]['wsyk']
            sbyk_sum = zy_data[1]['sbyk']
            dcyk_sum = zy_data[1]['dcyk']
            zxxm_sum = zy_data[1]['zxxm']
            sbxm_sum = zy_data[1]['sbxm']
            tmyk_sum = zy_data[1]['tmyk']
        end
        local dlzy_sum = wsyk_sum + sbyk_sum + dcyk_sum
        local khyk_sum = -(wsyk_sum + sbyk_sum + dcyk_sum + tmyk_sum)
        local tmcm_sum = tmbj + dlzy_sum + khyk_sum;
        data_tongji_zy.tmbj = tmbj --台面本金
        data_tongji_zy.tmcm = tmcm_sum --台面筹码
        data_tongji_zy.tmyk = tmyk_sum --台面盈亏
        data_tongji_zy.wsyk = wsyk_sum --尾数盈亏
        data_tongji_zy.sbyk = sbyk_sum --三宝盈亏
        data_tongji_zy.dcyk = dcyk_sum --对冲盈亏
        if math.floor(dlzy_sum) < dlzy_sum then
            dlzy_sum = dlzy_sum - dlzy_sum % 0.01
        end
        data_tongji_zy.dlzy = dlzy_sum --代理总赢
        data_tongji_zy.zxxm = zxxm_sum --庄闲洗码
        data_tongji_zy.sbxm = sbxm_sum --三宝洗码
        if math.floor(khyk_sum) < khyk_sum then
            khyk_sum = khyk_sum - khyk_sum % 0.01
        end
        data_tongji_zy.khyk = khyk_sum --客户盈亏
        send_data_to_admin(admin_uid, { cmd = 1002, set = 33, data = data_tongji_zy })
        local query_sql = string.format("insert into chat_history(card_game_id,chat_msg)values(%d,'%s')", card_game_id, json.encode(admin_msg_old))
        local insert_res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if insert_res ~= nil and insert_res.insert_id ~= nil then
            local delete_chat_id = insert_res.insert_id - 10
            local delete_query_sql = string.format("DELETE FROM `chat_history` WHERE id<%d", delete_chat_id)
            skynet.call("MG_SAVE", "lua", "update_sql", delete_query_sql)
        end
        odds_uid = {}
        odds_now = { 0, 0, 0, 0, 0, 0, 0, 0 }
        REQUEST.set(admin_uid, { set = 31 }) --推送用户列表
        REQUEST.set(admin_uid, { set = 32 }) --推送当局报表
        return
    end

    --取消此局
    if data.set == 5 then
        odds_begin_time = os.time()
        odds_begin_time_switch = 0
        packet.ret = 0
        --清除上局统计报表
        odds_now = { 0, 0, 0, 0, 0, 0, 0, 0 }
        uid_odds_msg_old = {}
        --下注退回
        local query_sql = 'begin;' --数据库事务开始
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        local bets_odds_uid = {}
        local query_sql = string.format("select * from bets_log where card_game_id=%d and state=0", card_game_id)
        local bets_rs = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if bets_rs ~= nil and bets_rs[1] ~= nil then
            for key, value in pairs(bets_rs) do
                value['uid'] = tonumber(value['uid'])
                if bets_odds_uid[value['uid']] == nil then
                    bets_odds_uid[value['uid']] = { 0, 0, 0, 0, 0, 0, score_old = PLAYR_OTHER[value['uid']].score_old, score = PLAYR_OTHER[value['uid']].score, ai = value['user_ai'], tourist = value['tourist'], win = 0 }
                    bets_odds_uid[value['uid']][value['type']] = value['odds']
                    bets_odds_uid[value['uid']]['win'] = -value['odds']
                elseif bets_odds_uid[value['uid']][value['type']] == nil then
                    bets_odds_uid[value['uid']][value['type']] = value['odds']
                    bets_odds_uid[value['uid']]['win'] = bets_odds_uid[value['uid']]['win'] - value['odds']
                else
                    bets_odds_uid[value['uid']][value['type']] = value['odds'] + bets_odds_uid[value['uid']][value['type']]
                    bets_odds_uid[value['uid']]['win'] = bets_odds_uid[value['uid']]['win'] - value['odds']
                end
            end
        end

        for i, v in pairs(bets_odds_uid) do
            local odds = 0
            for ii = 1, 5 do
                if v[ii] ~= nil and v[ii] > 0 then
                    odds = odds + v[ii]
                end
            end
            if odds > 0 then
                local db_data = {}
                db_data.uid = i
                db_data.card_game_id = card_game_id
                db_data.time = os.date("%Y-%m-%d %H:%M:%S")
                db_data.type = 112 --取消此局
                db_data.score = PLAYR_OTHER[i].score
                db_data.score_change = odds
                db_data.score_after = odds + PLAYR_OTHER[i].score
                db_data.user_ai = PLAYR_OTHER[i].ai
                db_data.tourist = PLAYR_OTHER[i].tourist
                db_data.orderid = card_game_id
                db_data.ukey = card_game_id .. '_' .. i .. '_112'
                local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
                if user_score_log_id > 0 then
                    local query_sql = string.format("update user set score = score + %f where uid = %d", odds, i)
                    skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                end

                PLAYR_OTHER[i].score = PLAYR_OTHER[i].score + odds
                if PLAYR_OTHER[i].ai == 0 and PLAYR_OTHER[i].tourist == 0 then
                    score_all = score_all + odds
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                elseif PLAYR_OTHER[i].ai == 0 and PLAYR_OTHER[i].tourist == 1 then
                    tourist_score_all = tourist_score_all + odds
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                end
                REQUEST.c2s_bjl_user_money(i)
            end
        end


        local query_sql = string.format("update `bets_log` set state=2 where card_game_id = %d", card_game_id)
        local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if res and res.errno then
            local query_sql = 'rollback;' --数据库事务回退
            local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '数据库异常'
            packet.ret = 4
            send_data(uid, packet)
            return
        end

        local query_sql = string.format("update `card_game` set state=2 where id = %d", card_game_id)
        local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if res and res.errno then
            local query_sql = 'rollback;' --数据库事务回退
            local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '数据库异常'
            packet.ret = 4
            send_data(uid, packet)
            return
        end

        local query_sql = 'commit;' --数据库事务执行
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

        room_data.ju = room_data.ju + 1

        room_status = 0
        packet.room_status = room_status
        packet.msg = '本局已被管理员取消，本局下注全部无效，已退款'
        send_all(packet)
        --odds_uid初始化
        odds_uid = {}
        REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
        REQUEST.set(admin_uid, { set = 32 }) --推送当局报表
    end

    --开牌
    if data.set == 6 and room_status == 2 then
        odds_begin_time = os.time()
        odds_begin_time_switch = 0
        packet.pai = data.pai
        packet.index = data.w
        packet.room_info = {
            odds_stop_time = odds_stop_time_new,
            odds_begin_time = odds_begin_time_cur
        }
        packet.ret = 0
        packet.msg = nil
        send_all(packet)
        return
    end

    --手动开奖  手动增加路单
    if data.set == 7 and room_status == 0 then
        if data.zd == nil then
            data.zd = 0
        end
        if data.xd == nil then
            data.xd = 0
        end
        local ukey = data.boots_number .. '_' .. data.ju .. '_' .. os.date("%Y%m%d%H%M")
        local time = os.date("%Y-%m-%d %H:%M:%S")
        local query_sql = string.format("insert into `card_game` (room_id, boots_number,ju,time,zhuang,zhuang_dui,xian_dui,state,ukey) values( %d, %d, %d, '%s', %d, %d, %d, 1,'%s')", data.room_id, data.boots_number, data.ju, time, data.y, data.zd, data.xd, ukey)
        card_game_id = skynet.call("MG_SAVE", "lua", "inser_card_game", nil, query_sql)
        room_data.ju = room_data.ju + 1
        room_status = 0
        local arr = {}
        arr.y = data.y
        arr.zd = data.zd
        arr.xd = data.xd
        if ludan_card_game_id[card_game_id] == nil then
            ludan_card_game_id[card_game_id] = true
            table.insert(ludan, arr)
        end

        ludan_da_update()
        for uid, _ in pairs(PLAYR_OTHER) do
            if uid ~= admin_uid then
                send_user_info(uid, PLAYR_OTHER[uid], 1)
            end
        end
        return
    end

    --牌局结果 重新结算
    if data.set == 100 and data.type == 2 and data.gameID ~= nil then
        data.gameID = math.floor(data.gameID)
        local zhuang_win = 0
        local zhuang_dui = 0
        local xian_dui = 0
        if data.y ~= nil then
            zhuang_win = data.y
        end
        if data.zd ~= nil then
            zhuang_dui = data.zd
        end
        if data.xd ~= nil then
            xian_dui = data.xd
        end
        --获取路单数据
        local query_sql = "select * from card_game where id=" .. data.gameID
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)
        if rss.data ~= nil and rss.data ~= '' then
            rss.data = json.decode(rss.data)
        end
        local odds_now_old = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        if rss.text ~= nil then
            rss.text = json.decode(rss.text)
            odds_now_old = rss.text.odds_now
        end
        local tmbj = room_data.capital --台面本金
        local tmcm = 0 --台面筹码
        local tmyk = 0 --台面盈亏
        local wsyk = 0 --尾数盈亏
        local sbyk = 0 --三宝盈亏
        local dcyk = 0 --对冲盈亏
        local dlzy = 0 --代理总赢
        local zxxm = 0 --庄闲洗码
        local sbxm = 0 --三宝洗码
        local khyk = 0 --客户盈亏
        uid_odds_msg_old = {}

        local zhuang_total = odds_now_old[1] --庄的下注总额
        local xian_total = odds_now_old[2] --闲的下注总额
        local dc_total = math.min(odds_now_old[1], odds_now_old[2]) --对冲的金额
        local mantissa = math.floor(room_data.mantissa) --尾数吃码向下取整
        local tm = 0
        local ws = 0
        if odds_now_old[1] ~= 0 or odds_now_old[2] ~= 0 then
            if mantissa > 0 then
                tm = (math.floor(math.abs(odds_now_old[1] - odds_now_old[2]) / mantissa)) * mantissa --推码
            elseif mantissa == 0 then
                tm = math.abs(odds_now_old[1] - odds_now_old[2]) --推码
            end
            if tm < room_data.odds_zx_min then
                tm = 0
            end
            ws = math.abs(odds_now_old[1] - odds_now_old[2]) - tm --尾数
        end

        --对冲盈亏
        if zhuang_win == 1 then
            dcyk = dcyk + dc_total - (dc_total * room_data.zhuang)
        end

        --统计尾数盈亏，庄闲洗码
        if zhuang_total > xian_total then
            --庄的总额大于闲，尾数，推码下庄
            if zhuang_win == 1 then
                wsyk = -(ws * room_data.zhuang)
                tmyk = -(tm * room_data.zhuang)
            elseif zhuang_win == 2 then
                wsyk = ws
                zxxm = tm
                tmyk = tm
            end
        elseif zhuang_total < xian_total then
            --庄的总额小于闲，尾数，推码下闲
            if zhuang_win == 1 then
                wsyk = ws
                zxxm = tm
                tmyk = tm
            elseif zhuang_win == 2 then
                wsyk = -(ws * room_data.xian)
                tmyk = -(tm * room_data.xian)
            end
        end

        --统计三宝洗码，三宝盈亏
        local zd_total = 0 --庄对投注总额
        local xd_total = 0 --闲对投注总额
        local he_total = 0 --和投注总额
        if odds_now_old[4] ~= nil and odds_now_old[4] > 0 then
            zd_total = odds_now_old[4]
        end
        if odds_now_old[5] ~= nil and odds_now_old[5] > 0 then
            xd_total = odds_now_old[5]
        end
        if odds_now_old[3] ~= nil and odds_now_old[3] > 0 then
            he_total = odds_now_old[3]
        end
        local sb_total = zd_total + xd_total + he_total --三宝总额

        if zhuang_win == 3 then
            sbyk = sbyk - he_total * room_data.he
        else
            sbyk = sbyk + he_total
            sbxm = sbxm + he_total
        end

        if zhuang_dui == 4 then
            sbyk = sbyk - zd_total * room_data.zhuang_dui
        else
            sbyk = sbyk + zd_total
            sbxm = sbxm + zd_total
        end

        if xian_dui == 5 then
            sbyk = sbyk - xd_total * room_data.xian_dui
        else
            sbyk = sbyk + xd_total
            sbxm = sbxm + xd_total
        end

        --统计代理总赢
        dlzy = wsyk + sbyk + dcyk
        --客户盈亏
        khyk = -(wsyk + sbyk + dcyk + tmyk)
        --台面筹码
        tmcm = tmbj + dlzy + khyk

        local rs = win_result_recount(data.gameID, zhuang_win, zhuang_dui, xian_dui, rss.text, zhuang_total, xian_total, he_total, sb_total, zd_total, xd_total, dc_total, ws, tm, tmbj, tmcm, tmyk, wsyk, sbyk, dcyk, dlzy, zxxm, sbxm, khyk)
    end
    --牌局结果修改
    if data.set == 101 then
        if data.zd == nil then
            data.zd = 0
        end
        if data.xd == nil then
            data.xd = 0
        end
        local zhuang_dui = 0
        local xian_dui = 0
        if data.zd == 4 then
            zhuang_dui = 1
        end
        if data.xd == 5 then
            xian_dui = 1
        end
        local query_sql = string.format("update `card_game` set zhuang=%d,zhuang_dui=%d,xian_dui=%d where id=%d", data.y, zhuang_dui, xian_dui, data.gameID)
        skynet.call("MG_SAVE", "lua", "update_sql", query_sql)

        --获取路单数据
        local query_sql = "select * from card_game where zhuang > 0 and state != 2 and deleted=0 and id > (select id from card_game where start = 1 order by id desc limit 1) order by id asc"
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
        ludan = {}
        ludan_card_game_id = {}
        if rss ~= false then
            for i, v in pairs(rss) do
                local arr = {}
                arr.y = v.zhuang
                arr.zd = v.zhuang_dui
                arr.xd = v.xian_dui
                ludan_card_game_id[v.id] = true
                table.insert(ludan, arr)
            end
        end
        ludan_da_update()
        for uid, _ in pairs(PLAYR_OTHER) do
            if uid ~= admin_uid then
                send_user_info(uid, PLAYR_OTHER[uid], 1)
            end
        end
        return
    end

    --靴号加1，路单重启
    if data.set == 102 then
        if data.boots_number ~= nil then
            room_data.boots_number = data.boots_number
        end
        if data.ju ~= nil then
            room_data.ju = data.ju
        end
        local query_sql = string.format("select id from card_game where state !=0 order by id desc limit 1")
        local gameInfo = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        local gameID = 0
        if gameInfo ~= nil and gameInfo[1] ~= nil then
            gameID = gameInfo[1]['id']
        end
        local sql = string.format("update card_game set start=1 where id=%d", gameID)
        skynet.call("MG_SAVE", "lua", "update_sql", sql)
        ludan = {}
        ludan_new = {} --珠子路
        ludan_sx = {} --三星路
        ludan_da_new = {} --大路
        ludan_dyz = {} --大眼仔路
        ludan_jy = {} --曱甴路
        ludan_xiao = {} --小路规则
        ludan_yc = { { dyz = 0, xl = 0, yyl = 0 }, { dyz = 0, xl = 0, yyl = 0 } } --路单预测1庄2闲
        ludan_num_count = { 0, 0, 0, 0, 0, 0, 0 } --牌局结果数量
        for uid, _ in pairs(PLAYR_OTHER) do
            if uid ~= admin_uid then
                send_user_info(uid, PLAYR_OTHER[uid], 1)
            end
        end
        return
    end

    --露珠起点、删除牌局，路单更新
    if data.set == 103 then
        --获取路单数据
        local query_sql = "select * from card_game where zhuang > 0 and state != 2 and deleted=0 and id > (select id from card_game where start = 1 order by id desc limit 1) order by id asc"
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
        ludan = {}
        ludan_card_game_id = {}
        if rss ~= false then
            for i, v in pairs(rss) do
                local arr = {}
                arr.y = v.zhuang
                arr.zd = v.zhuang_dui
                arr.xd = v.xian_dui
                ludan_card_game_id[v.id] = true
                table.insert(ludan, arr)
            end
        end
        ludan_da_update()
        for uid, _ in pairs(PLAYR_OTHER) do
            if uid ~= admin_uid then
                send_user_info(uid, PLAYR_OTHER[uid], 1)
            end
        end
        return
    end

    --牌局消息
    if data.set == 8 and data.room_id ~= nil and data.boots_number ~= nil and data.ju ~= nil then
        room_data.room_id = data.room_id
        room_data.boots_number = data.boots_number
        room_data.ju = data.ju
        local db_data = {}
        db_data.uid = uid
        db_data.time = os.date("%Y-%m-%d %H:%M:%S")
        db_data.room_id = data.room_id
        db_data.boots_number = data.boots_number
        db_data.ju = data.ju
        db_data.ukey = data.boots_number .. '_' .. data.ju .. '_' .. os.date("%Y%m%d%H%M")
        card_game_id = skynet.call("MG_SAVE", "lua", "inser_card_game", db_data)
    end

    --当前牌局
    if data.set == 21 then
        packet.room_status = room_status
        packet.room_id = room_id
        packet.boots_number = boots_number
        packet.ju = ju
        send_all(packet)
        return
    end
    --会员列表
    if data.set == 31 then
        --设置机器人,总余分，机器人余分变化
        if data.lei ~= nil and data.lei == 5 and data.update_uid ~= nil and data.ai ~= nil then

            if PLAYR_OTHER[data.update_uid] ~= nil then
                PLAYR_OTHER[data.update_uid].ai = data.ai
            end

            local user_info = skynet.call("MG_SAVE", "lua", "get_user", data.update_uid)
            if user_info.score ~= nil and user_info.score ~= 0 then
                if data.ai == 0 then
                    score_all = score_all + user_info.score
                    score_old_all = score_old_all + user_info.score
                    ai_score_all = ai_score_all - user_info.score
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                elseif data.ai == 1 then
                    score_all = score_all - user_info.score
                    score_old_all = score_old_all - user_info.score
                    ai_score_all = ai_score_all + user_info.score
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                end
            end
        end

        --设置游客,总余分，游客余分变化
        if data.lei ~= nil and data.lei == 3 and data.update_uid ~= nil and data.tourist ~= nil then

            if PLAYR_OTHER[data.update_uid] ~= nil then
                PLAYR_OTHER[data.update_uid].tourist = data.tourist
            end

            local user_info = skynet.call("MG_SAVE", "lua", "get_user", data.update_uid)
            if user_info.score ~= nil and user_info.score ~= 0 then
                if data.tourist == 0 then
                    score_all = score_all + user_info.score
                    score_old_all = score_old_all + user_info.score
                    tourist_score_all = tourist_score_all - user_info.score
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                elseif data.tourist == 1 then
                    score_all = score_all - user_info.score
                    score_old_all = score_old_all - user_info.score
                    tourist_score_all = tourist_score_all + user_info.score
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                end
            end
        end

        if data.update_uid ~= nil and data.update_uid > 0 and PLAYR_OTHER[data.update_uid] ~= nil then
            --后台修改用户各个属性状态
            local userInfo = skynet.call("MG_SAVE", "lua", "getUserInfo", data.update_uid)
            if userInfo ~= nil and userInfo[1] ~= nil then
                PLAYR_OTHER[data.update_uid].status = userInfo[1]['status']
                PLAYR_OTHER[data.update_uid].deleted = userInfo[1]['deleted']
                PLAYR_OTHER[data.update_uid].no_say = userInfo[1]['no_say']
                PLAYR_OTHER[data.update_uid].no_xi = userInfo[1]['no_xi']
            else
                PLAYR_OTHER[data.update_uid] = nil
                table.remove(PLAYR_OTHER, data.update_uid)
            end
            REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
        end

        local csf = 0
        if data.csf ~= nil and data.csf > 0 then
            csf = 1 --后台初始分操作
            score_all = skynet.call("MG_SAVE", "lua", "get_user_score_all")
            score_old_all = score_all
            ai_score_all = skynet.call("MG_SAVE", "lua", "get_user_score_all_ai")
            tourist_score_all = skynet.call("MG_SAVE", "lua", "get_user_score_all_tourist")
            local query_sql_01 = string.format("update  `score_all` set start=0,mark=1 where start=1 ")
            local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql_01)
            if res.affected_rows > 0 then
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 1, card_game_id)
            end
        end

        local data = {}
        local key = 0
        for i, v in pairs(PLAYR_OTHER) do
            if i ~= admin_uid and PLAYR_OTHER[i].name ~= nil then
                key = key + 1
                local arr = {}
                arr.key = key
                arr.name = PLAYR_OTHER[i].name
                arr.uid = i
                local user_score_old = 0
                if PLAYR_OTHER[i].score_old ~= nil then
                    user_score_old = PLAYR_OTHER[i].score_old
                else
                    user_score_old = PLAYR_OTHER[i].score
                end
                --odds_uid初始化
                if odds_uid[i] == nil then
                    odds_uid[i] = { 0, 0, 0, 0, 0, 0, score_old = user_score_old, score = PLAYR_OTHER[i].score, ai = PLAYR_OTHER[i].ai, tourist = PLAYR_OTHER[i].tourist, win = 0 }
                end
                arr.csf = math.floor(PLAYR_OTHER[i].score_old)
                if PLAYR_OTHER[i].ai == 1 and PLAYR_OTHER[i].score_old == nil then
                    arr.csf = math.floor(PLAYR_OTHER[i].score)
                end
                if csf == 1 then
                    PLAYR_OTHER[i].score_old = math.floor(PLAYR_OTHER[i].score)
                    odds_uid[i].score_old = math.floor(PLAYR_OTHER[i].score)
                    arr.csf = math.floor(PLAYR_OTHER[i].score)
                end
                arr.yue = math.floor(PLAYR_OTHER[i].score)
                arr.isfalse = PLAYR_OTHER[i].ai
                arr.istourist = PLAYR_OTHER[i].tourist
                arr.lei1 = odds_uid[i][1]
                arr.lei2 = odds_uid[i][2]
                arr.lei3 = odds_uid[i][3]
                arr.lei4 = odds_uid[i][4]
                arr.lei5 = odds_uid[i][5]
                table.insert(data, arr)
            end
        end

        local score_all_data = skynet.call("MG_SAVE", "lua", "select_score_all")
        if score_all_data ~= nil and score_all_data[1] ~= nil then
            score_all = score_all_data[1]['score_all']
            score_old_all = score_all_data[1]['score_old_all']
            ai_score_all = score_all_data[1]['ai_score_all']
            tourist_score_all = score_all_data[1]['tourist_score_all']
        end
        score_all = skynet.call("MG_SAVE", "lua", "get_user_score_all")
        packet.data = data
        packet.score_all = score_all
        packet.score_old_all = score_old_all
        packet.ai_score_all = ai_score_all
        packet.tourist_score_all = tourist_score_all

        send_data_to_admin(admin_uid, packet)
        return
    end

    --当局报表
    if data.set == 32 then
        local bets_odds_now = { 0, 0, 0, 0, 0, 0, 0, 0 }
        local query_sql = string.format("card_game_id=%d and state=0", card_game_id)
        local bets_rs = skynet.call("MG_SAVE", "lua", "get_bets_log", query_sql)
        if bets_rs ~= nil then
            for key, value in pairs(bets_rs) do
                if value['user_ai'] == 0 and value['tourist'] == 0 then
                    bets_odds_now[value['type']] = bets_odds_now[value['type']] + value['odds']
                    if value['type'] >= 3 then
                        bets_odds_now[6] = bets_odds_now[6] + value['odds']
                    end
                end
            end
        end
        local data_tongji = {
            z = 0,
            x = 0,
            h = 0,
            zd = 0,
            xd = 0,
            sb = 0,
            tm = 0,
            ws = 0
        }
        data_tongji.z = bets_odds_now[1]
        data_tongji.x = bets_odds_now[2]
        data_tongji.h = bets_odds_now[3]
        data_tongji.zd = bets_odds_now[4]
        data_tongji.xd = bets_odds_now[5]
        data_tongji.sb = bets_odds_now[6]
        local mantissa = math.floor(room_data.mantissa) --尾数吃码向下取整
        local tm = 0
        local dj_ws = 0 --尾数
        if bets_odds_now[1] ~= 0 or bets_odds_now[2] ~= 0 then
            if mantissa > 0 then
                tm = (math.floor(math.abs(bets_odds_now[1] - bets_odds_now[2]) / mantissa)) * mantissa --推码
            elseif mantissa == 0 then
                tm = math.abs(bets_odds_now[1] - bets_odds_now[2]) --推码
            end
            if tm < room_data.odds_zx_min then
                tm = 0
            end
            dj_ws = math.abs(bets_odds_now[1] - bets_odds_now[2]) - tm
        end

        data_tongji.tm = tm
        if math.floor(dj_ws) < dj_ws then
            data_tongji.ws = dj_ws - dj_ws % 0.01
        else
            data_tongji.ws = dj_ws
        end
        send_data_to_admin(admin_uid, { cmd = 1002, set = data.set, data = data_tongji })
        return
    end

    --自营报表
    if data.set == 33 then
        local tmbj = room_data.capital --台面本金
        local data_tongji_zy = {}
        local zy_data = skynet.call("MG_SAVE", "lua", "get_zy_tongji")
        local wsyk_sum = 0
        local sbyk_sum = 0
        local dcyk_sum = 0
        local zxxm_sum = 0
        local sbxm_sum = 0
        local tmyk_sum = 0
        if zy_data ~= nil and zy_data[1] ~= nil and zy_data[1]['wsyk'] ~= nil then
            wsyk_sum = zy_data[1]['wsyk']
            sbyk_sum = zy_data[1]['sbyk']
            dcyk_sum = zy_data[1]['dcyk']
            zxxm_sum = zy_data[1]['zxxm']
            sbxm_sum = zy_data[1]['sbxm']
            tmyk_sum = zy_data[1]['tmyk']
        end
        local dlzy_sum = wsyk_sum + sbyk_sum + dcyk_sum
        local khyk_sum = -(wsyk_sum + sbyk_sum + dcyk_sum + tmyk_sum)
        local tmcm_sum = tmbj + dlzy_sum + khyk_sum;
        data_tongji_zy.tmbj = tmbj --台面本金
        data_tongji_zy.tmcm = tmcm_sum --台面筹码
        data_tongji_zy.tmyk = tmyk_sum --台面盈亏
        data_tongji_zy.wsyk = wsyk_sum --尾数盈亏
        data_tongji_zy.sbyk = sbyk_sum --三宝盈亏
        data_tongji_zy.dcyk = dcyk_sum --对冲盈亏
        if math.floor(dlzy_sum) < dlzy_sum then
            dlzy_sum = dlzy_sum - dlzy_sum % 0.01
        end
        data_tongji_zy.dlzy = dlzy_sum --代理总赢
        data_tongji_zy.zxxm = zxxm_sum --庄闲洗码
        data_tongji_zy.sbxm = sbxm_sum --三宝洗码
        if math.floor(khyk_sum) < khyk_sum then
            khyk_sum = khyk_sum - khyk_sum % 0.01
        end
        data_tongji_zy.khyk = khyk_sum --客户盈亏
        send_data_to_admin(admin_uid, { cmd = 1002, set = data.set, data = data_tongji_zy })
        return
    end

    --归零保存自营报表
    if data.set == 104 then
        --保存自营报表数据
        local tmbj = room_data.capital --台面本金
        local data_tongji_zy = {}
        local zy_data = skynet.call("MG_SAVE", "lua", "get_zy_tongji")
        local wsyk_sum = 0
        local sbyk_sum = 0
        local dcyk_sum = 0
        local zxxm_sum = 0
        local sbxm_sum = 0
        local tmyk_sum = 0
        if zy_data ~= nil and zy_data[1] ~= nil and zy_data[1]['wsyk'] ~= nil then
            wsyk_sum = zy_data[1]['wsyk']
            sbyk_sum = zy_data[1]['sbyk']
            dcyk_sum = zy_data[1]['dcyk']
            zxxm_sum = zy_data[1]['zxxm']
            sbxm_sum = zy_data[1]['sbxm']
            tmyk_sum = zy_data[1]['tmyk']
        end
        local dlzy_sum = wsyk_sum + sbyk_sum + dcyk_sum
        local khyk_sum = -(wsyk_sum + sbyk_sum + dcyk_sum + tmyk_sum)
        local tmcm_sum = tmbj + dlzy_sum + khyk_sum;
        data_tongji_zy.tmbj = tmbj --台面本金
        data_tongji_zy.tmcm = tmcm_sum --台面筹码
        data_tongji_zy.tmyk = tmyk_sum --台面盈亏
        data_tongji_zy.wsyk = wsyk_sum --尾数盈亏
        data_tongji_zy.sbyk = sbyk_sum --三宝盈亏
        data_tongji_zy.dcyk = dcyk_sum --对冲盈亏
        if math.floor(dlzy_sum) < dlzy_sum then
            dlzy_sum = dlzy_sum - dlzy_sum % 0.01
        end
        data_tongji_zy.dlzy = dlzy_sum --代理总赢
        data_tongji_zy.zxxm = zxxm_sum --庄闲洗码
        data_tongji_zy.sbxm = sbxm_sum --三宝洗码
        if math.floor(khyk_sum) < khyk_sum then
            khyk_sum = khyk_sum - khyk_sum % 0.01
        end
        data_tongji_zy.khyk = khyk_sum --客户盈亏
        data_tongji_zy.score_all = skynet.call("MG_SAVE", "lua", "get_user_score_all") --用户余额
        data_tongji_zy.upfen_all = 0
        data_tongji_zy.dowfen_all = 0
        --获取开工时间
        local query_sql = string.format("select time from `card_game` where guiling=0 and state =1 limit 1 ")
        local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        local data = {}
        data.begin_time = os.date("%Y-%m-%d %H:%M:%S")
        data.end_time = os.date("%Y-%m-%d %H:%M:%S")
        if res ~= nil and res[1] ~= nil then
            data.begin_time = res[1]['time']
        end
        --上分总额
        data.type = '11,20,13'
        local upfen_res = skynet.call("MG_SAVE", "lua", "get_fen_all_by_type", data)
        if upfen_res ~= nil and upfen_res[1] ~= nil and upfen_res[1]['money_change'] ~= nil then
            data_tongji_zy.upfen_all = upfen_res[1]['money_change']
        end
        --下分总额
        data.type = '12,21'
        local dowfen_res = skynet.call("MG_SAVE", "lua", "get_fen_all_by_type", data)
        if dowfen_res ~= nil and dowfen_res[1] ~= nil and dowfen_res[1]['money_change'] ~= nil then
            data_tongji_zy.dowfen_all = dowfen_res[1]['money_change']
        end
        local insert_id = skynet.call("MG_SAVE", "lua", "insert_zy_tongji", data_tongji_zy)
        local query_sql = string.format("update `card_game` set guiling=1 where guiling=0 and state=1")
        skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        return
    end

    if data.set == 105 then
        --后台删除机器人
        if data.uid ~= nil and PLAYR_OTHER[data.uid] ~= nil then
            table.remove(PLAYR_OTHER, data.uid)
            REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
        end
    end
end

function REQUEST.up(uid, data)
    local packet = {
        cmd = NetCmd.S2C_BJL_ODDS,
        uid = uid,
        ret = 1,
        --0 游戏还没开始  1 可以下注 2 停止下注
        room_status = room_status,
        --房间人数
        room_num = room_data.num,
    }
    if data.name == nil then
        data.name = name
    end
    if data.phone_num == nil then
        data.phone_num = phone_num
    end
    local query_sql = string.format("update user set name = '%s',phone_num = '%s' where uid = %d", data.name, data.phone_num, uid)
    local rs = skynet.call("MG_SAVE", "lua", "update_user", uid, nil, nil, query_sql)
    PLAYR_OTHER[uid].phone_num = data.phone_num
    PLAYR_OTHER[uid].name = data.name
    PLAYR_OTHER[uid].score = data.score
    packet.ret = 0
    send_data(uid, packet)
end

function REQUEST.c2s_bjl_log(uid, data)
    local packet = {
        cmd = NetCmd.S2C_BJL_LOG,
        uid = uid,
        set = data.set,
        ret = 1,
    }
    local data = {}
    -- local query_sql = string.format("uid = %d order by id desc limit 0,20", uid)
    -- local rs = skynet.call("MG_SAVE", "lua", "get_bets_log", query_sql)

    -- for i, v in pairs(rs) do
    --     local arr = {}
    --     arr.time = v.time
    --     arr.odds = v.odds
    --     arr.rate = v.rate
    --     arr.win = v.win
    --     table.insert(data, arr)
    -- end

    -- SELECT str_to_date(time, '%Y-%m-%d') ,sum(odds)

    -- local query_sql = string.format("select count(1) as num from bets_log where uid = %d", uid)
    -- local rss = skynet.call("MG_SAVE", "lua", "mysql_get_one",query_sql)
    -- local all_num = 0
    -- if rss.num ~= nil then
    --     all_num = rss.num

    local query_sql = "SELECT str_to_date(time, '%Y-%m-%d') as date,card_game_id,sum(if(win > 0,win,-odds)) as win_all FROM `bets_log` where state = 1 and uid = " .. uid .. " group by card_game_id order by card_game_id asc"
    local rs = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
    local tmp = {}
    for i, v in pairs(rs) do
        if tmp[v.date] == nil then
            tmp[v.date] = { win_all = 0, card_game_win = 0, card_game_all = 0 }
        end
        tmp[v.date].win_all = tmp[v.date].win_all + v.win_all
        tmp[v.date].card_game_all = tmp[v.date].card_game_all + 1
        if v.win_all > 0 then
            tmp[v.date].card_game_win = tmp[v.date].card_game_win + 1
        end
    end
    for i, v in pairs(tmp) do
        local arr = {}
        arr.time = i
        arr.rate = math.floor(v.card_game_win / v.card_game_all * 100) / 100
        if math.floor(arr.rate) < arr.rate then
            arr.rate = arr.rate - arr.rate % 0.01
        end
        arr.win = v.win_all
        table.insert(data, arr)
    end
    -- end

    packet.data = data
    packet.ret = 0
    send_data(uid, packet)
end


--下注记录
function REQUEST.c2s_bjl_odds_log(uid, data)
    local packet = {
        cmd = NetCmd.S2C_BJL_ODDS_LOG,
        uid = uid,
        set = data.set,
        ret = 1,
    }
    --七品要求去掉用户下注记录
    local port = tonumber(skynet.getenv("port"))
    if port == 5253 or port == 5152 then
        packet.data = {}
        packet.ret = 0
        send_data(uid, packet)
        return
    end

    local query_sql = string.format("select card_game_id from `bets_log` where state=1 and uid=%d group by card_game_id  order by id desc  limit 30", uid)
    local card_game_rs = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    local data = {}
    if #card_game_rs > 0 then
        local card_game_id_str = ''
        local card_game_id_table = {}
        for i, v in pairs(card_game_rs) do
            card_game_id_str = card_game_id_str .. v.card_game_id .. ','
            table.insert(card_game_id_table, v.card_game_id)
        end
        local card_game_id_str_sql = string.sub(card_game_id_str, 1, #card_game_id_str - 1)
        local query_sql = string.format("select b.uid,b.card_game_id,b.type,b.odds,b.score,b.win,b.state,b.time,c.room_id,c.boots_number,c.ju from `bets_log` as b left join card_game  as c on b.card_game_id=c.id where b.card_game_id in (%s) and b.deleted=0 and b.state=1 and uid=%d and b.type !=6 order by b.id desc", card_game_id_str_sql, uid)
        local rs = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        local arr = {}
        for i, v in pairs(rs) do
            if arr[v.card_game_id] == nil then
                arr[v.card_game_id] = {
                    time = string.sub(v.time, 6),
                    odds = v.odds,
                    win = v.win,
                    table_name = v.room_id .. '桌' .. v.boots_number .. '-' .. v.ju .. '局',
                    last_ju_score = v.score,
                    current_ju_score = v.score + v.win,
                    odds_text = { 0, 0, 0, 0, 0 },
                }
                arr[v.card_game_id]['odds_text'][v.type] = v.odds
                if math.floor(arr[v.card_game_id].current_ju_score) < arr[v.card_game_id].current_ju_score then
                    arr[v.card_game_id].current_ju_score = arr[v.card_game_id].current_ju_score - arr[v.card_game_id].current_ju_score % 0.01
                end
            else
                if v.score > arr[v.card_game_id].last_ju_score then
                    arr[v.card_game_id].last_ju_score = v.score
                end
                arr[v.card_game_id].win = arr[v.card_game_id].win + v.win
                if math.floor(arr[v.card_game_id].win) < arr[v.card_game_id].win then
                    arr[v.card_game_id].win = arr[v.card_game_id].win - arr[v.card_game_id].win % 0.01
                end
                arr[v.card_game_id].current_ju_score = arr[v.card_game_id].last_ju_score + arr[v.card_game_id].win
                if math.floor(arr[v.card_game_id].current_ju_score) < arr[v.card_game_id].current_ju_score then
                    arr[v.card_game_id].current_ju_score = arr[v.card_game_id].current_ju_score - arr[v.card_game_id].current_ju_score % 0.01
                end
                arr[v.card_game_id]['odds_text'][v.type] = arr[v.card_game_id]['odds_text'][v.type] + v.odds
            end
        end

        for i, v in pairs(card_game_id_table) do
            if arr[v] ~= nil and arr[v]['odds_text'] ~= nil then
                for key = 1, #arr[v]['odds_text'] do
                    if arr[v]['odds_text'][key] ~= nil and arr[v]['odds_text'][key] > 0 then
                        if arr[v]['type'] == nil then
                            arr[v]['type'] = get_odds_type(key) .. '/' .. arr[v]['odds_text'][key] .. ' '
                        else
                            arr[v]['type'] = arr[v]['type'] .. get_odds_type(key) .. '/' .. arr[v]['odds_text'][key] .. ' '
                        end
                    end
                end
            end
            table.insert(data, arr[v])
        end
    end
    packet.data = data
    packet.ret = 0
    send_data(uid, packet)
end

--粉丝列表
function REQUEST.c2s_bjl_agent(uid, data)
    local packet = {
        cmd = NetCmd.S2C_BJL_AGENT,
        uid = uid,
        ret = 0,
    }

    --一级粉丝
    local data = {}
    local query_sql = string.format("select * from user where agent_id = %d ", uid)
    local rs = skynet.call("MG_SAVE", "lua", "get_user", 0, query_sql)
    for i, v in pairs(rs) do
        local arr = {}
        arr.name = v.name
        arr.head = v.head
        arr.uid = v.uid
        table.insert(data, arr)
    end

    --二级粉丝
    local lower = {}
    local query_sql_second = string.format("select * from user where agent_id in(select uid from user where agent_id=%d)", uid)
    local rsecond = skynet.call("MG_SAVE", "lua", "get_user", 0, query_sql_second)
    for i, value in pairs(rsecond) do
        local arr = {}
        arr.name = value.name
        arr.head = value.head
        arr.uid = value.uid
        table.insert(lower, arr)
    end
    packet.data = data
    packet.lower = lower
    send_data(uid, packet)
end

function get_odds_type(odds_type)
    local switch = {
        [1] = function()
            return '庄'
        end,
        [2] = function()
            return '闲'
        end,
        [3] = function()
            return '和'
        end,
        [4] = function()
            return '庄对'
        end,
        [5] = function()
            return '闲对'
        end,
        [6] = function()
            return '三宝'
        end,
    }
    local fSwitch = nil
    local rs = ''
    fSwitch = switch[odds_type]
    if fSwitch then
        --key exists
        rs = fSwitch() --do func
    end
    return rs
end

--获取赔率
function get_rate(odds_type, flag)
    local switch = {
        [1] = function()
            return room_data.zhuang
        end,
        [2] = function()
            return room_data.xian
        end,
        [3] = function()
            return room_data.he
        end,
        [4] = function()
            return room_data.zhuang_dui
        end,
        [5] = function()
            return room_data.xian_dui
        end,
    }
    local fSwitch = nil
    local rs = 0
    fSwitch = switch[odds_type]
    if fSwitch then
        --key exists
        rs = fSwitch() --do func
    end
    if flag ~= nil then
        --key exists
        rs = rs + flag
    end
    return rs
end

--获取投注记录
function get_bets_log(uid, zhuang_win, zhuang_dui, xian_dui)
    local bets_odds_now = { 0, 0, 0, 0, 0, 0, 0, 0 }
    local bets_data = {}
    local query_sql
    if uid ~= nil and uid > 0 then
        query_sql = string.format("card_game_id=%d and state=0 and uid=%d", card_game_id, uid)
    else
        query_sql = string.format("card_game_id=%d and state=0", card_game_id)
    end
    local bets_rs = skynet.call("MG_SAVE", "lua", "get_bets_log", query_sql)
    if bets_rs ~= nil then
        for key, value in pairs(bets_rs) do
            local uid = tonumber(value['uid'])
            --PLAYR_OTHER初始化
            if PLAYR_OTHER[uid] == nil then
                local query_sql = string.format("SELECT name,head,score,ai,tourist FROM `user` WHERE uid=%d", uid)
                local user_info_res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                local user_info = user_info_res[1]
                PLAYR_OTHER[uid] = {
                    uid = uid,
                    name = user_info.name,
                    head = user_info.head,
                    score = user_info.score,
                    score_old = user_info.score,
                    ai = user_info.ai,
                    tourist = user_info.tourist,
                }
            end
            if value['user_ai'] == 0 and value['tourist'] == 0 then
                bets_odds_now[value['type']] = bets_odds_now[value['type']] + value['odds']
                if value['type'] >= 3 then
                    bets_odds_now[6] = bets_odds_now[6] + value['odds']
                end
            end
            if bets_data[value['uid']] == nil then
                bets_data[value['uid']] = {
                    0, 0, 0, 0, 0, 0,
                    score_old = PLAYR_OTHER[uid].score_old,
                    score = PLAYR_OTHER[uid].score,
                    ai = value['user_ai'],
                    tourist = value['tourist'],
                    win = 0
                }
                bets_data[value['uid']][value['type']] = value['odds']
                bets_data[value['uid']]['win'] = -value['odds']
            elseif bets_data[value['uid']][value['type']] == nil then
                bets_data[value['uid']][value['type']] = value['odds']
                bets_data[value['uid']]['win'] = bets_data[value['uid']]['win'] - value['odds']
            else
                bets_data[value['uid']][value['type']] = value['odds'] + bets_data[value['uid']][value['type']]
                bets_data[value['uid']]['win'] = bets_data[value['uid']]['win'] - value['odds']
            end
        end
    end
    --下注合并
    if zhuang_win ~= nil and zhuang_win > 0 and zhuang_dui ~= nil and xian_dui ~= nil then
        for key, value in pairs(bets_data) do
            local uid = tonumber(key)
            if PLAYR_OTHER[uid]['ai'] == 0 then
                local db_data = {
                    uid = uid,
                    nickname = PLAYR_OTHER[uid]['name'],
                    zhuang = value[1],
                    xian = value[2],
                    he = value[3],
                    zhuang_dui = value[4],
                    xian_dui = value[5],
                    score_before = PLAYR_OTHER[uid]['score'] - value['win'],
                    score_after = PLAYR_OTHER[uid]['score'],
                    win = value['win'],
                    game_zhuang = zhuang_win,
                    game_zhuang_dui = zhuang_dui,
                    game_xian_dui = xian_dui,
                    state = 0,
                    mktime = os.time(),
                    card_game_id = card_game_id,
                    room_id = room_data.room_id,
                    boots_number = room_data.boots_number,
                    ju = room_data.ju,
                }
                skynet.call("MG_SAVE", "lua", "bets_log_merge", db_data)
            end
        end
    end
    return bets_odds_now, bets_data
end

--三宝最小限红
function sb_min_odds(sb_odds, uid)
    if sb_odds < room_data.sb_min_odds then
        local packet = {
            cmd = NetCmd.S2C_BJL_ODDS,
            uid = uid,
            msg = '@' .. PLAYR_OTHER[uid].name .. '本次下注无效，三宝最低下注' .. room_data.sb_min_odds,
            color = 2,
            is_login = 0,
            name = PLAYR_OTHER[admin_uid].name,
            head = PLAYR_OTHER[admin_uid].head,
            time = os.date("%Y-%m-%d %H:%M:%S"),
            ret = 0,
            card_game_id = card_game_id,
            score = PLAYR_OTHER[uid].score,
            cur_time = os.date("%m-%d %H:%M:%S"),
            only_uid = uid
        }
        send_data(uid, packet)
        send_data_to_admin(admin_uid, packet)
        return 1
    else
        return 0
    end
end

--取消下注
function REQUEST.c2s_bjl_odds_cancel(uid, data)

    local packet = {
        cmd = NetCmd.S2C_BJL_ODDS_CANCEL,
        uid = uid,
        ret = 0,
    }
    if room_status ~= 1 then
        send_data(uid, packet)
        return
    end
    local redis_setnx_key = skynet.getenv("port") .. '_odds_' .. uid
    local redis_result = skynet.call('MG_SAVE', 'lua', 'redis_setnx', redis_setnx_key, 1, 60)
    if redis_result == nil then
        packet.cmd = NetCmd.S2C_BJL_ODDS
        packet.msg = '@' .. PLAYR_OTHER[uid].name .. '您有注单正在处理，请稍后再试'
        packet.color = 2
        packet.ret = 0
        packet.only_uid = uid
        send_data(uid, packet)
        return
    end
    local query_sql = 'begin;' --数据库事务开始
    skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
    local bets_log_id_str
    local bets_odds_uid = {}
    local odds_cancel_score = 0
    local query_sql = string.format("select * from bets_log where card_game_id=%d and state=0 and uid=%d", card_game_id, uid)
    local bets_rs = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if bets_rs ~= nil and bets_rs[1] ~= nil then
        for key, value in pairs(bets_rs) do
            if bets_log_id_str == nil then
                bets_log_id_str = value['id']
            else
                bets_log_id_str = bets_log_id_str .. ',' .. value['id']
            end
            if bets_odds_uid[value['uid']] == nil then
                bets_odds_uid[value['uid']] = { 0, 0, 0, 0, 0, 0, score_old = PLAYR_OTHER[value['uid']].score_old, score = PLAYR_OTHER[value['uid']].score_old, ai = value['user_ai'], tourist = value['tourist'], win = 0 }
                bets_odds_uid[value['uid']][value['type']] = value['odds']
                bets_odds_uid[value['uid']]['win'] = -value['odds']
            elseif bets_odds_uid[value['uid']][value['type']] == nil then
                bets_odds_uid[value['uid']][value['type']] = value['odds']
                bets_odds_uid[value['uid']]['win'] = bets_odds_uid[value['uid']]['win'] - value['odds']
            else
                bets_odds_uid[value['uid']][value['type']] = value['odds'] + bets_odds_uid[value['uid']][value['type']]
                bets_odds_uid[value['uid']]['win'] = bets_odds_uid[value['uid']]['win'] - value['odds']
            end
        end
        local odds_arr = bets_odds_uid[uid]
        local query_sql = string.format("SELECT  score,ai,tourist FROM `user` WHERE uid=%d", uid)
        local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        local info = user_info[1]
        local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
        if score_log ~= nil and score_log[1] ~= nil then
            if score_log[1]['score_after'] ~= info.score and PLAYR_OTHER[uid].tourist == 0 and PLAYR_OTHER[uid].ai == 0 then
                local db_data = {}
                db_data.uid = uid
                db_data.score = info.score
                db_data.score_log = score_log[1]['score_after']
                db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
                skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
                local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
                local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            end
            info.score = score_log[1]['score_after']
        end
        for i = 1, 6 do
            if odds_arr[i] ~= nil and odds_arr[i] > 0 then
                odds_now[i] = odds_now[i] - odds_arr[i]
                if i < 6 then
                    odds_cancel_score = odds_cancel_score + odds_arr[i]
                end
            end
        end

        local query_sql = string.format("update bets_log set state = 3 where id in(%s)", bets_log_id_str)
        skynet.call("MG_SAVE", "lua", "update_sql", query_sql)

        local db_data = {}
        db_data.uid = uid
        db_data.card_game_id = card_game_id
        db_data.time = os.date("%Y-%m-%d %H:%M:%S")
        db_data.type = 110 --取消下注返还
        db_data.score = info.score
        db_data.score_change = odds_cancel_score
        db_data.score_after = info.score + odds_cancel_score
        db_data.user_ai = info.ai
        db_data.tourist = info.tourist
        db_data.orderid = card_game_id
        db_data.ukey = bets_log_id_str .. '_110'
        local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
        if user_score_log_id > 0 then
            local query_sql = string.format("update user set score=score+%f where uid=%d", odds_cancel_score, uid)
            skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        else
            odds_cancel_score = 0
            local query_sql = 'rollback;' --数据库事务回退
            local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '网络异常,请重新操作'
            packet.ret = 2
            send_data(uid, packet)
            return
        end

        local query_sql = 'commit;' --数据库事务执行
        skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

        if PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 0 then
            score_all = score_all + odds_cancel_score
            skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
        elseif PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 1 then
            tourist_score_all = tourist_score_all + odds_cancel_score
            skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
        end
        PLAYR_OTHER[uid].score = info.score + odds_cancel_score
    end

    --odds_uid初始化
    odds_uid[uid] = { 0, 0, 0, 0, 0, 0, score_old = PLAYR_OTHER[uid].score_old, score = PLAYR_OTHER[uid].score, ai = PLAYR_OTHER[uid].ai, tourist = PLAYR_OTHER[uid].tourist, win = 0 }
    REQUEST.c2s_bjl_user_money(uid)
    local packet = {
        cmd = NetCmd.S2C_BJL_ODDS,
        msg = '撤销以下下注。',
        color = 2,
        is_login = 0,
        name = PLAYR_OTHER[uid].name,
        head = PLAYR_OTHER[uid].head,
        time = os.date("%Y-%m-%d %H:%M:%S"),
        ret = 0,
        card_game_id = card_game_id,
        score = 0,
        cur_time = os.date("%m-%d %H:%M:%S"),
    }
    if odds_cancel_score > 0 then
        packet.only_uid = uid
        send_data(uid, packet)
        local packet_t = clone(packet)
        packet_t.name = PLAYR_OTHER[admin_uid].name
        packet_t.head = PLAYR_OTHER[admin_uid].head
        packet_t.msg = '@' .. PLAYR_OTHER[uid].name .. '撤销以下下注。'
        packet_t.cur_time = os.date("%m-%d %H:%M:%S")
        send_data_to_admin(admin_uid, packet_t)
    else
        local packet_t = clone(packet)
        packet_t.name = PLAYR_OTHER[admin_uid].name
        packet_t.head = PLAYR_OTHER[admin_uid].head
        packet_t.msg = '@' .. PLAYR_OTHER[uid].name .. '暂无下注记录。'
        packet_t.cur_time = os.date("%m-%d %H:%M:%S")
        send_data(uid, packet_t)
    end
    --解锁
    skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
    REQUEST.set(admin_uid, { set = 31 })
    REQUEST.set(admin_uid, { set = 32 })
end

--用户下注
function REQUEST.odds(uid, data)
    --预防card_game插入失败，card_game_id=0 bug
    if card_game_id == 0 then
        local query_sql = string.format("SELECT id FROM `card_game` where state=0 ORDER BY id DESC limit 1")
        local card_game_res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        card_game_id = card_game_res[1]['id']
    end
    local start = 3
    local odds = 0
    local odds_type = 0
    local cur_time = os.date("%Y-%m-%d %H:%M:%S")
    local packet = {
        cmd = NetCmd.S2C_BJL_ODDS,
        uid = uid,
        msg = data.msg,
        color = 1,
        is_login = 0,
        name = PLAYR_OTHER[uid].name,
        head = PLAYR_OTHER[uid].head,
        time = cur_time,
        ret = 1,
        card_game_id = card_game_id,
        score = PLAYR_OTHER[uid].score,
        cur_time = os.date("%m-%d %H:%M:%S"),
    }
    if uid == admin_uid then
        packet.color = 2
    end
    --消息记录
    local chat_log = {
        uid = uid,
        msg = data.msg,
        time = packet.time,
        card_game_id = card_game_id,
        room_id = room_data.room_id,
        boots_number = room_data.boots_number,
        ju = room_data.ju,
        type = 1,
        score = PLAYR_OTHER[uid].score
    }

    if data.odds_msg ~= nil and string.sub(data.odds_msg, 1, 2) == 'xf' then
        chat_log.msg = data.odds_msg
        chat_log.type = 2
        --插入聊天记录
        if PLAYR_OTHER[uid].ai == 0 then
            skynet.call('MG_SAVE', 'lua', 'insert_chat_log', chat_log)
        end
        odds = string.sub(data.odds_msg, 3, string.len(data.odds_msg))
        odds = tonumber(odds)
        if odds ~= nil and odds > 0 then
            odds = math.floor(math.abs(odds))
        else
            odds = 0
        end
        if odds > 0 then
            local data_rss = {}
            data_rss.money = odds
            data_rss.money_type = 1
            data_rss.yh_name = ''
            data_rss.yh_hao = ''
            REQUEST.c2s_bjl_xia_fen(uid, data_rss)
            return
        end
    end
    if data.odds_msg ~= nil and string.sub(data.odds_msg, 1, 2) == 'sf' then
        chat_log.msg = data.odds_msg
        chat_log.type = 3
        --插入聊天记录
        if PLAYR_OTHER[uid].ai == 0 then
            skynet.call('MG_SAVE', 'lua', 'insert_chat_log', chat_log)
        end
        odds = string.sub(data.odds_msg, 3, string.len(data.odds_msg))
        odds = tonumber(odds)
        if odds ~= nil and odds > 0 then
            odds = math.floor(math.abs(odds))
        else
            odds = 0
        end
        if odds > 0 then
            local data_rss = {}
            data_rss.fen = odds
            data_rss.channel = 1
            REQUEST.c2s_bjl_upfen(uid, data_rss)
            return
        end
    end
    local room_status_stop_key = skynet.getenv("port") .. '_room_status_stop'
    local redis_result = skynet.call('MG_SAVE', 'lua', 'redis_exists', room_status_stop_key)
    if redis_result then
        packet.msg = '已停止下注，本次下注无效'
        packet.color = 2
        packet.ret = 4
        packet.only_uid = uid
        send_data(uid, packet)
        return
    end
    if data.odds_msg ~= nil and room_status == 2 then
        packet.msg = '已停止下注，本次下注无效'
        packet.color = 2
        packet.ret = 4
        packet.only_uid = uid
        send_data(uid, packet)
        return
    end


    if data.odds_msg ~= nil and room_status == 1 then
        local redis_setnx_key = skynet.getenv("port") .. '_odds_' .. uid
        local redis_result = skynet.call('MG_SAVE', 'lua', 'redis_setnx', redis_setnx_key, 1, 60)
        if redis_result == nil then
            packet.msg = '@' .. PLAYR_OTHER[uid].name .. '您有注单正在处理，请稍后再试'
            packet.color = 2
            packet.ret = 0
            packet.only_uid = uid
            send_data(uid, packet)
            return
        end

        chat_log.msg = data.odds_msg
        chat_log.type = 4
        --插入聊天记录
        if PLAYR_OTHER[uid].ai == 0 then
            local chat_log_id = skynet.call('MG_SAVE', 'lua', 'insert_chat_log', chat_log)
            if chat_log_id == 0 then
                --解锁
                skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                packet.msg = '网络异常,请重新下注'
                packet.color = 2
                packet.ret = 4
                send_data(uid, packet)
                return
            end
        end
        local odds_arr = split(data.odds_msg, ',')
        local is_suoha_zhuang = 0
        local is_suoha_xian = 0
        local odds_msg_str = ''
        local odds_arr_new_tmp = { 0, 0, 0, 0, 0, 0 } --用户下注数组
        --用户下注数据初始化
        if odds_uid[uid] == nil then
            odds_uid[uid] = { 0, 0, 0, 0, 0, 0, win = 0 }
        end
        for i, v in pairs(odds_arr) do
            start = 3
            local msg_str = v
            odds = string.sub(msg_str, start, string.len(msg_str))
            odds = tonumber(odds)
            if odds ~= nil and odds > 0 then
                odds = math.floor(math.abs(odds))
            else
                odds = 0
            end
            if string.sub(msg_str, 1, 2) == 'zd' or string.sub(msg_str, 1, 2) == 'ZD' then
                odds_arr_new_tmp[4] = odds_arr_new_tmp[4] + odds
            elseif string.sub(msg_str, 1, 2) == 'xd' or string.sub(msg_str, 1, 2) == 'XD' then
                odds_arr_new_tmp[5] = odds_arr_new_tmp[5] + odds
            elseif string.sub(msg_str, 1, 2) == 'sb' or string.sub(msg_str, 1, 2) == 'SB' then
                odds_arr_new_tmp[3] = odds_arr_new_tmp[3] + odds
                odds_arr_new_tmp[4] = odds_arr_new_tmp[4] + odds
                odds_arr_new_tmp[5] = odds_arr_new_tmp[5] + odds
                odds_arr_new_tmp[6] = odds_arr_new_tmp[6] + odds
            elseif string.sub(msg_str, 1, 2) == 'zs' or string.sub(msg_str, 1, 2) == 'ZS' and is_suoha_zhuang == 0 then
                odds_arr_new_tmp = { 0, 0, 0, 0, 0, 0 }
                local query_sql = string.format("SELECT  score FROM `user` WHERE uid=%d", uid)
                local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                local info = user_info[1]
                odds = math.floor(info.score)
                if odds == 0 then
                    packet.msg = '余分不足，下注无效。'
                    packet.color = 2
                    packet.ret = 4
                    send_data(uid, packet)
                    --解锁
                    skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                    return
                end
                is_suoha_zhuang = 1
                odds_arr_new_tmp[1] = odds
                break
            elseif string.sub(msg_str, 1, 2) == 'xs' or string.sub(msg_str, 1, 2) == 'XS' and is_suoha_xian == 0 then
                odds_arr_new_tmp = { 0, 0, 0, 0, 0, 0 }
                local query_sql = string.format("SELECT  score FROM `user` WHERE uid=%d", uid)
                local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                local info = user_info[1]
                odds = math.floor(info.score)
                is_suoha_xian = 1
                odds_arr_new_tmp[2] = odds
                if odds == 0 then
                    packet.msg = '余分不足，下注无效。'
                    packet.color = 2
                    packet.ret = 4
                    send_data(uid, packet)
                    --解锁
                    skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                    return
                end
                break
            else
                start = 2
                odds = string.sub(msg_str, start, string.len(msg_str))
                odds = tonumber(odds)
                if odds ~= nil and odds > 0 then
                    odds = math.floor(math.abs(odds))
                else
                    odds = 0
                end
                if string.sub(msg_str, 1, 1) == 'z' or string.sub(msg_str, 1, 1) == 'Z' then
                    if is_suoha_zhuang == 0 then
                        odds_arr_new_tmp[1] = odds_arr_new_tmp[1] + odds
                    end
                elseif string.sub(msg_str, 1, 1) == 'x' or string.sub(msg_str, 1, 1) == 'X' then
                    if is_suoha_xian == 0 then
                        odds_arr_new_tmp[2] = odds_arr_new_tmp[2] + odds
                    end
                elseif string.sub(msg_str, 1, 1) == 'h' or string.sub(msg_str, 1, 1) == 'H' then
                    odds_arr_new_tmp[3] = odds_arr_new_tmp[3] + odds
                elseif string.sub(msg_str, 1, 1) == 'd' or string.sub(msg_str, 1, 1) == 'D' then
                    odds_arr_new_tmp[4] = odds_arr_new_tmp[4] + odds
                    odds_arr_new_tmp[5] = odds_arr_new_tmp[5] + odds
                end
            end
        end
        local odds_arr_new_all = 0
        for i, v in pairs(odds_arr_new_tmp) do
            if i ~= 6 then
                odds_arr_new_all = odds_arr_new_all + v
            end
            if v > 0 then
                if i == 1 then
                    odds_msg_str = odds_msg_str .. '庄' .. v .. ','
                elseif i == 2 then
                    odds_msg_str = odds_msg_str .. '闲' .. v .. ','
                elseif i == 3 then
                    local tmpp = v - odds_arr_new_tmp[6]
                    if tmpp > 0 then
                        odds_msg_str = odds_msg_str .. '和' .. tmpp .. ','
                    end
                elseif i == 4 then
                    local tmpp = v - odds_arr_new_tmp[6]
                    if tmpp > 0 then
                        odds_msg_str = odds_msg_str .. '庄对' .. tmpp .. ','
                    end
                elseif i == 5 then
                    local tmpp = v - odds_arr_new_tmp[6]
                    if tmpp > 0 then
                        odds_msg_str = odds_msg_str .. '闲对' .. tmpp .. ','
                    end
                elseif i == 6 then
                    odds_msg_str = odds_msg_str .. '三宝' .. v .. ','
                end
            end
        end
        local tmp_s = math.abs(odds_now[1] + odds_arr_new_tmp[1] - odds_now[2] - odds_arr_new_tmp[2])
        if room_data.mantissa > 0 then
            tmp_s = math.floor(tmp_s / room_data.mantissa) * room_data.mantissa
        end
        if tmp_s > room_data.single then
            packet.msg = '下注金额大于台面单边限红'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        --庄闲个人单次最小限红
        if odds_arr_new_tmp[1] > 0 and odds_arr_new_tmp[1] < room_data.odds_zx_min then
            packet.msg = '庄闲单个下注金额不能小于' .. room_data.odds_zx_min
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        if odds_arr_new_tmp[2] > 0 and odds_arr_new_tmp[2] < room_data.odds_zx_min then
            packet.msg = '庄闲单个下注金额不能小于' .. room_data.odds_zx_min
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        --三宝个人单次最小下注
        if odds_arr_new_tmp[3] > 0 and odds_arr_new_tmp[3] < room_data.odds_sb_min then
            packet.msg = '三宝单个下注金额不能小于' .. room_data.odds_sb_min
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        if odds_arr_new_tmp[4] > 0 and odds_arr_new_tmp[4] < room_data.odds_sb_min then
            packet.msg = '三宝单个下注金额不能小于' .. room_data.odds_sb_min
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        if odds_arr_new_tmp[5] > 0 and odds_arr_new_tmp[5] < room_data.odds_sb_min then
            packet.msg = '三宝单个下注金额不能小于' .. room_data.odds_sb_min
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        --庄闲个人累计最大限红
        if odds_uid[uid][1] + odds_arr_new_tmp[1] > room_data.odds_zx_max then
            packet.msg = '超过个人庄闲限红，本次下注无效'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            return
        end
        if odds_uid[uid][2] + odds_arr_new_tmp[2] > room_data.odds_zx_max then
            packet.msg = '超过个人庄闲限红，本次下注无效'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        --三宝个人累计最大限红
        if odds_uid[uid][3] + odds_arr_new_tmp[3] > room_data.odds_sb_max then
            packet.msg = '超过个人三宝限红，本次下注无效'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        if odds_uid[uid][4] + odds_arr_new_tmp[4] > room_data.odds_sb_max then
            packet.msg = '超过个人三宝限红，本次下注无效'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        if odds_uid[uid][5] + odds_arr_new_tmp[5] > room_data.odds_sb_max then
            packet.msg = '超过个人三宝限红，本次下注无效'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        --三宝累计总额限红
        if odds_uid[uid][3] + odds_uid[uid][4] + odds_uid[uid][5] + odds_arr_new_tmp[3] + odds_arr_new_tmp[4] + odds_arr_new_tmp[5] > room_data.odds_sb_all then
            packet.msg = '超过三宝总额限红，本次下注无效'
            packet.color = 2
            packet.ret = 3
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end

        local query_sql = string.format("SELECT  score FROM `user` WHERE uid=%d", uid)
        local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        local info = user_info[1]
        local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
        if score_log ~= nil and score_log[1] ~= nil then
            if score_log[1]['score_after'] ~= info.score and PLAYR_OTHER[uid].tourist == 0 and PLAYR_OTHER[uid].ai == 0 then
                local db_data = {}
                db_data.uid = uid
                db_data.score = info.score
                db_data.score_log = score_log[1]['score_after']
                db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
                skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
                local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
                local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            end
            info.score = score_log[1]['score_after']
        end
        if info.score == nil or info.score < odds_arr_new_all then
            packet.msg = '本次下注无效,账号余额不足,请先上分'
            packet.color = 2
            packet.ret = 4
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
        local query_sql = string.format("UPDATE user SET score=score-%.2f where uid = %d", odds_arr_new_all, uid)
        local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if res and res.errno then
            packet.msg = '本次下注无效,网络异常'
            packet.color = 2
            packet.ret = 4
            packet.only_uid = uid
            send_data(uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end

        PLAYR_OTHER[uid].score = info.score
        local before_odds_score = info.score --下注前余分
        local db_data = {}
        for i, v in pairs(odds_arr_new_tmp) do
            if v > 0 and i ~= 6 then
                db_data = {}
                db_data.uid = uid
                db_data.card_game_id = card_game_id
                db_data.time = cur_time
                db_data.text = data.odds_msg
                db_data.type = i
                db_data.odds = v
                db_data.score = before_odds_score
                db_data.rate = get_rate(i)
                db_data.user_ai = PLAYR_OTHER[uid].ai
                db_data.tourist = PLAYR_OTHER[uid].tourist
                db_data.score_change = -v
                db_data.score_after = before_odds_score - v
                local bets_log_id = skynet.call("MG_SAVE", "lua", "inser_bets_log", db_data)
                if bets_log_id > 0 then
                    before_odds_score = before_odds_score - v
                    --余分流水
                    db_data.orderid = bets_log_id
                    db_data.ukey = bets_log_id .. '_' .. i
                    skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
                else
                    packet.msg = '网络异常，本次下注无效。'
                    packet.color = 2
                    packet.ret = 4
                    packet.only_uid = uid
                    send_data(uid, packet)
                    --解锁
                    skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                    return
                end
            end
        end

        PLAYR_OTHER[uid].score = before_odds_score

        --机器人和游客下注，不上统计报表
        for odds_type, odds in pairs(odds_arr_new_tmp) do
            if odds > 0 then
                if PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 0 then
                    if odds_type == 6 then
                        odds_now[odds_type] = odds_now[odds_type] + odds * 3
                    else
                        odds_now[odds_type] = odds_now[odds_type] + odds
                    end
                end

                if odds_uid[uid] == nil then
                    odds_uid[uid] = { 0, 0, 0, 0, 0, 0, score_old = PLAYR_OTHER[uid].score_old, score = PLAYR_OTHER[uid].score, ai = PLAYR_OTHER[uid].ai, tourist = PLAYR_OTHER[uid].tourist, win = 0 }
                end
                if odds_uid[uid][odds_type] == nil then
                    odds_uid[uid][odds_type] = 0
                end
                odds_uid[uid][odds_type] = odds_uid[uid][odds_type] + odds
            end
        end
        odds_uid[uid].win = odds_uid[uid].win - odds_arr_new_all
        --总分更新
        if PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 0 then
            score_all = score_all - odds_arr_new_all
            skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
        elseif PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 1 then
            tourist_score_all = tourist_score_all - odds_arr_new_all
            skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
        end
        odds_msg_str = string.sub(odds_msg_str, 1, -2)
        packet.ret = 0
        packet.msg = odds_msg_str
        packet.odds_msg = data.odds_msg
        packet.score = PLAYR_OTHER[uid].score
        send_data(uid, packet)

        packet.score = nil
        send_other(uid, packet)

        if odds_uid[uid][1] > 0 and odds_uid[uid][2] > 0 and odds_uid[uid].msg_send == nil then
            odds_uid[uid].msg_send = 1
            packet.color = 2
            packet.msg = '@' .. PLAYR_OTHER[uid].name .. ' 庄闲对打'
            packet.name = PLAYR_OTHER[admin_uid].name
            packet.head = PLAYR_OTHER[admin_uid].head
            packet.only_uid = uid
            packet.cur_time = os.date("%m-%d %H:%M:%S"),
            send_data(uid, packet)
            send_data_to_admin(admin_uid, packet)
        end

        packet.color = 2
        packet.msg = '@' .. PLAYR_OTHER[uid].name .. '  下注成功'
        packet.name = PLAYR_OTHER[admin_uid].name
        packet.head = PLAYR_OTHER[admin_uid].head
        packet.only_uid = uid
        packet.cur_time = os.date("%m-%d %H:%M:%S")
        send_data(uid, packet)
        send_data_to_admin(admin_uid, packet)

        uid_odds_last = uid
        uid_odds_last_msg = odds_msg_str
        REQUEST.c2s_bjl_user_money(uid)

        REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
        REQUEST.set(admin_uid, { set = 32 }) --推送当局报表
        --解锁
        skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
        return
    end
    --插入聊天记录
    if PLAYR_OTHER[uid].ai == 0 and uid ~= admin_uid and chat_log.msg ~= nil then
        skynet.call('MG_SAVE', 'lua', 'insert_chat_log', chat_log)
    end
    if PLAYR_OTHER[uid].no_say ~= nil and PLAYR_OTHER[uid].no_say == 0 then
        packet.color = 6
        if data.msg ~= nil then
            packet.ret = 0
            packet.msg = data.msg
            table.insert(uid_odds_msg_old, packet)
            send_all(packet)
        end
        if data.odds_msg ~= nil then
            packet.ret = 0
            packet.msg = data.odds_msg
            table.insert(uid_odds_msg_old, packet)
            send_all(packet)
        end
    elseif PLAYR_OTHER[uid].no_say ~= nil and PLAYR_OTHER[uid].no_say == 1 then
        packet.msg = '您当前无法发言，请联系客服处理。' .. PLAYR_OTHER[uid].no_say
        send_data(uid, packet)
    end

    if data.admin_msg ~= nil then
        packet.color = 2
        packet.ret = 0
        packet.msg = data.admin_msg
        packet.isadmin = true
        table.insert(uid_odds_msg_old, packet)
        send_all(packet)
    end
end

function odss_log(uid)
    if uid_odds_last > 0 then
        local packet = {
            cmd = NetCmd.S2C_BJL_ODDS,
            msg = uid_odds_last_msg,
            color = 1,
            uid = uid_odds_last,
            name = PLAYR_OTHER[uid_odds_last].name,
            head = PLAYR_OTHER[uid_odds_last].head,
            time = os.date("%Y-%m-%d %H:%M:%S"),
            ret = 0,
            is_login = 1,
            card_game_id = card_game_id,
            score = PLAYR_OTHER[uid].score,
        }

        local data = {}
        local key = 0
        for i, v in pairs(PLAYR_OTHER) do
            if i ~= admin_uid then
                if odds_uid[i] ~= nil and odds_uid[i][1] + odds_uid[i][2] + odds_uid[i][3] + odds_uid[i][4] + odds_uid[i][5] > 0 then
                    key = key + 1
                    local arr = {}
                    arr.key = key
                    arr.name = PLAYR_OTHER[i].name
                    arr.uid = key

                    arr.z = odds_uid[i][1]
                    arr.x = odds_uid[i][2]
                    arr.h = odds_uid[i][3]
                    arr.zd = odds_uid[i][4]
                    arr.xd = odds_uid[i][5]
                    arr.sb = odds_uid[i][6]
                    if i == uid then
                        local msg_arr = {}
                        for iii = 1, 6 do
                            if odds_uid[i][iii] > 0 then
                                local arr_tmp = ''
                                if iii == 1 then
                                    arr_tmp = 'z'
                                elseif iii == 2 then
                                    arr_tmp = 'x'
                                elseif iii == 3 then
                                    arr_tmp = 'h'
                                elseif iii == 4 then
                                    arr_tmp = 'zd'
                                elseif iii == 5 then
                                    arr_tmp = 'xd'
                                elseif iii == 6 then
                                    arr_tmp = 'sb'
                                end
                                arr_tmp = arr_tmp .. odds_uid[i][iii]
                                table.insert(msg_arr, arr_tmp)
                            end
                        end
                        packet.msg_arr = msg_arr
                    end
                    table.insert(data, arr)
                end
            end
        end
        if #data > 0 then
            packet.data = data
            packet.score = PLAYR_OTHER[uid].score
            send_data(uid, packet)
        end
    end
end

function getHongLan(len1, len2, istiao)
    if istiao == 1 then
        if len1 == nil or len1 < 1 then
            return nil
        end
        if len1 == len2 then
            return 1
        else
            return 2
        end
    else
        if len1 >= len2 then
            return 1
        elseif len1 + 1 < len2 then
            return 1
        else
            return 2
        end
    end
end

function dataToList(arr)
    local z = arr[1]
    local data = {}
    local lis = {}
    for i = 1, #arr do
        if z ~= arr[i] then
            local xlie = clone(lis)
            table.insert(data, lis)
            lis = {}
            z = arr[i]
        end
        table.insert(lis, { y = arr[i] })
    end
    table.insert(data, lis)
    return data
end

function data_List_6(data)
    local xdata = {}
    for i = 1, #data do
        local wei1 = i;
        local hang = 6;
        for ii = 1, #data[i] do
            local wei2 = ii
            if wei2 > hang then
                wei2 = hang
                wei1 = wei1 + 1
            end
            if xdata[wei1] ~= nil and xdata[wei1][wei2] ~= nil and xdata[wei1][wei2].y ~= 0 then
                if wei2 <= hang then
                    wei2 = wei2 - 1
                    hang = wei2
                end
                wei1 = wei1 + 1
            end
            local z = clone(data[i][ii])
            if xdata[wei1] == nil then
                xdata[wei1] = { { y = 0 }, { y = 0 }, { y = 0 }, { y = 0 }, { y = 0 }, { y = 0 } }
            end
            xdata[wei1][wei2] = clone(z)
        end
    end
    return xdata
end

function ludan_da_update()
    ludan_da = {} --大路
    ludan_da_new = {} --大路
    ludan_dyz = {} --大眼仔路
    ludan_jy = {} --曱甴路
    ludan_xiao = {} --小路规则
    ludan_sx = {} --三星规则
    ludan_yc = { { dyz = 0, xl = 0, yyl = 0 }, { dyz = 0, xl = 0, yyl = 0 } } --路单预测1庄2闲
    local key_da = 1
    local key_num = 0
    local old_da = 0
    local key_da_key = 1
    local ludan_da_old = {}

    ludan_da = {}
    ludan_da_old[1] = {}

    ludan_da[1] = {}
    for iik = 1, 6 do
        ludan_da[1][iik] = { y = 0, zd = 0, xd = 0, num = 0 }
    end

    local key_da = 1
    local old_da = 0
    local key_da_key = 1
    local key_da_key_old = 0
    local key_da_he = 1 --第几列
    local key_da_he_len = 1 --每列长度
    local begin_he_total = 0 --前几局都是开和的数量
    local ludan_da_old_lie = 1 --列
    ludan_num_count = { 0, 0, 0, 0, 0, 0, 0 }
    for i, v in pairs(ludan) do
        --统计总数
        if v.y > 0 then
            if v.y == 1 then
                ludan_num_count[1] = ludan_num_count[1] + 1
            elseif v.y == 2 then
                ludan_num_count[2] = ludan_num_count[2] + 1
            elseif v.y == 3 then
                ludan_num_count[3] = ludan_num_count[3] + 1
            end
            if v.zd == 1 then
                ludan_num_count[4] = ludan_num_count[4] + 1
            end
            if v.xd == 1 then
                ludan_num_count[5] = ludan_num_count[5] + 1
            end
            ludan_num_count[6] = ludan_num_count[6] + 1
        end

        if v.y < 3 then
            table.insert(ludan_sx, v)
        end

        if old_da > 0 and v.y == 3 then
            if ludan_da[key_da_he][key_da_he_len] ~= nil then
                ludan_da[key_da_he][key_da_he_len].zd = v.zd == 1 and 1 or ludan_da[key_da_he][key_da_he_len].zd
                ludan_da[key_da_he][key_da_he_len].xd = v.xd == 1 and 1 or ludan_da[key_da_he][key_da_he_len].xd
                ludan_da[key_da_he][key_da_he_len].num = ludan_da[key_da_he][key_da_he_len].num > 0 and ludan_da[key_da_he][key_da_he_len].num + 1 or 1
            end
        else
            if old_da > 0 and v.y ~= old_da then
                key_da = key_da + 1
                ludan_da_old_lie = ludan_da_old_lie + 1
                key_da_he = key_da
                key_da_key = 1
                key_da_he_len = key_da_key
                key_da_key_old = 0
                if ludan_da[key_da] == nil then
                    ludan_da[key_da] = {}
                    for iik = 1, 6 do
                        ludan_da[key_da][iik] = { y = 0, zd = 0, xd = 0, num = 0 }
                    end
                end
                ludan_da_old[key_da] = {}
            end

            if key_da_key > 6 or ludan_da[key_da][key_da_key].y ~= 0 then
                if key_da_key_old == 0 then
                    key_da_key_old = key_da_key - 1
                end
                local tt = key_da + key_da_key - key_da_key_old
                key_da_he = tt
                if ludan_da[tt] == nil then
                    ludan_da[tt] = {}
                    for iik = 1, 6 do
                        ludan_da[tt][iik] = { y = 0, zd = 0, xd = 0, num = 0 }
                    end
                end
                ludan_da[tt][key_da_key_old] = { y = v.y, zd = v.zd, xd = v.xd, num = 0 }
                key_da_he_len = key_da_key_old
            else
                if v.y ~= 3 then
                    ludan_da[key_da][key_da_key] = { y = v.y, zd = v.zd, xd = v.xd, num = begin_he_total }
                    key_da_he_len = key_da_key
                    begin_he_total = 0
                    if begin_he_total > 0 then
                        ludan_da_old_lie = ludan_da_old_lie + 1
                    end
                else
                    begin_he_total = begin_he_total + 1
                end
            end

            if old_da > 0 or v.y ~= 3 then
                key_da_key = key_da_key + 1
                old_da = v.y
                table.insert(ludan_da_old[ludan_da_old_lie], v.y)
            end
        end
    end

    for i, v in pairs(ludan_da_old) do

        for ii = 0, #v - 1 do
            if i >= 2 then
                --大眼仔
                local dyzhl = nil;
                if ii == 0 then
                    if i > 2 then
                        dyzhl = getHongLan(#ludan_da_old[i - 2], #ludan_da_old[i - 1], 1)
                    end
                else
                    dyzhl = getHongLan(#ludan_da_old[i - 1], ii + 1, 0)
                end
                if dyzhl ~= nil then
                    table.insert(ludan_dyz, dyzhl)
                end
            end
            if i >= 3 then
                --小路规则
                local dyzhl = nil;
                if ii == 0 then
                    if i > 3 then
                        dyzhl = getHongLan(#ludan_da_old[i - 3], #ludan_da_old[i - 1], 1)
                    end
                else
                    dyzhl = getHongLan(#ludan_da_old[i - 2], ii + 1, 0)
                end
                if dyzhl ~= nil then
                    table.insert(ludan_xiao, dyzhl)
                end
            end
            if i >= 4 then
                --曱甴路
                local dyzhl = nil;
                if ii == 0 then
                    if i > 4 then
                        dyzhl = getHongLan(#ludan_da_old[i - 4], #ludan_da_old[i - 1], 1)
                    end
                else
                    dyzhl = getHongLan(#ludan_da_old[i - 3], ii + 1, 0)
                end
                if dyzhl ~= nil then
                    table.insert(ludan_jy, dyzhl)
                end
            end
            --下局提示 ,最后个数据
            if ludan_da_old ~= nil then
                if i == #ludan_da_old and ii == #ludan_da_old[i] - 1 then
                    --大眼仔
                    if i >= 2 then
                        local dyzhl_yc = getHongLan(#ludan_da_old[i - 1], ii + 2, 0);
                        if ludan_da_old[i][ii + 1] == 1 then
                            ludan_yc[1]['dyz'] = dyzhl_yc
                            ludan_yc[2]['dyz'] = dyzhl_yc % 2 == 0 and 1 or 2
                        elseif ludan_da_old[i][ii + 1] == 2 then
                            ludan_yc[2]['dyz'] = dyzhl_yc
                            ludan_yc[1]['dyz'] = dyzhl_yc % 2 == 0 and 1 or 2
                        end
                    end

                    --小路
                    if i >= 3 then
                        local xl_yc = getHongLan(#ludan_da_old[i - 2], ii + 2, 0);
                        if ludan_da_old[i][ii + 1] == 1 then
                            ludan_yc[1]['xl'] = xl_yc
                            ludan_yc[2]['xl'] = xl_yc % 2 == 0 and 1 or 2
                        elseif ludan_da_old[i][ii + 1] == 2 then
                            ludan_yc[2]['xl'] = xl_yc
                            ludan_yc[1]['xl'] = xl_yc % 2 == 0 and 1 or 2
                        end
                    end

                    --曱甴路
                    if i >= 4 then
                        local yylhl_yc = getHongLan(#ludan_da_old[i - 3], ii + 2, 0);
                        if ludan_da_old[i][ii + 1] == 1 then
                            ludan_yc[1]['yyl'] = yylhl_yc
                            ludan_yc[2]['yyl'] = yylhl_yc % 2 == 0 and 1 or 2
                        elseif ludan_da_old[i][ii + 1] == 2 then
                            ludan_yc[2]['yyl'] = yylhl_yc
                            ludan_yc[1]['yyl'] = yylhl_yc % 2 == 0 and 1 or 2
                        end
                    end
                end
            end
        end
    end

    local ludan_dyz_tt = dataToList(ludan_dyz)
    local ludan_dyztt = data_List_6(ludan_dyz_tt)
    ludan_dyz = er_wei_to_yi_wei(ludan_dyztt)

    local ludan_xiao_tt = dataToList(ludan_xiao)
    local ludan_xiaottt = data_List_6(ludan_xiao_tt)
    ludan_xiao = er_wei_to_yi_wei(ludan_xiaottt)

    local ludan_jy_tt = dataToList(ludan_jy)
    local ludan_jyttt = data_List_6(ludan_jy_tt)
    ludan_jy = er_wei_to_yi_wei(ludan_jyttt)
    ludan_da_new = er_wei_to_yi_wei(ludan_da)
    ludan_new = clone(ludan)
end

function er_wei_to_yi_wei(arr)
    local new_arr = {}
    for i, v in pairs(arr) do
        for ii, vv in pairs(v) do
            table.insert(new_arr, vv)
        end
    end
    return new_arr
end

function send_user_info(uid, data, flag)
    local web_domain = skynet.call('MG_SAVE', 'lua', 'get_web_domain')
    PLAYR_OTHER[uid].score = data.score
    local packet = {
        cmd = NetCmd.S2C_BJL_ROOM,
        uid = uid,
        ret = 0,
        user_info = {
            uid = uid,
            --钱
            money = data.money,
            --积分
            score = string.format('%.2f', PLAYR_OTHER[uid].score),
            --手机号码
            phone_num = data.phone_num,
            --昵称
            name = data.name .. "  ID:" .. uid,
            --头像
            head = data.head,
            --代理二维码
            agent_url = 'http://' .. web_domain['domain'] .. '/game.php?agent_id=' .. uid .. '&db_name=' .. skynet.getenv("database") .. '&redisdb=' .. skynet.getenv("redisdb"),
        },
        room_info = {
            notice = clone(notice_arr[uid]),
            --0 游戏还没开始  1 可以下注 2 停止下注
            room_status = room_status,
            --房间人数
            room_num = room_data.num,
            --最小下注
            odds_min = room_data.odds_zx_min,
            --最大下注
            odds_max = room_data.odds_zx_max,
            --直播地址
            live_address = room_data.live_address,
            --庄 赔率
            zhuang = room_data.zhuang,
            --闲 赔率
            xian = room_data.xian,
            --庄对 赔率
            zhuang_dui = room_data.zhuang_dui,
            --闲对 赔率
            xian_dui = room_data.xian_dui,
            --和 赔率
            he = room_data.he,
            --娱乐规则
            help_text = room_data.text,
            --代理说明
            agent = room_data.agent,
            --收款二维码
            money_code = room_data.money_code,
            --客服二维码
            customer = room_data.customer,
            --桌号
            room_code = room_data.room_id .. '桌' .. room_data.boots_number .. '-' .. room_data.ju .. '局',
            --代理二维码
            room_id = room_data.room_id,
            boots_number = room_data.boots_number,
            ju = room_data.ju,
            zhifubao_code = room_data.zhifubao_code,
            weixin_code = room_data.weixin_code,
            money_url = 'http://' .. web_domain['domain'] .. '/webPage/money?uid=' .. uid,
            --娱乐规则
            game_url = 'http://' .. web_domain['domain'] .. '/webPage/gameRule',
            report_url = 'http://mdh53.byleg.com/jubao/index.html',
            server_url = string.format('http://%s/server.php?db_name=%s', web_domain['domain'], skynet.getenv("database")),
            ludan_zz = ludan_new, --珠子路
            ludan_sx = ludan_sx, --三星路
            ludan_da = ludan_da_new, --大路
            ludan_dyz = ludan_dyz, --大眼仔路
            ludan_jy = ludan_jy, --曱甴路
            ludan_xiao = ludan_xiao, --小路规则
            ludan_yc = ludan_yc, --路单预测
            ludan_data = { ju = ludan_num_count[6], z = ludan_num_count[1], x = ludan_num_count[2], h = ludan_num_count[3], zd = ludan_num_count[4], xd = ludan_num_count[5] }, --局数13 庄5 闲5 和3 庄对3 闲对3
        }
    }
    if odds_begin_time_switch == 1 then
        packet.room_info.odds_begin_time = os.time() - odds_begin_time
    else
        packet.room_info.odds_begin_time = 0
    end
    packet.room_info.odds_stop_time = 0
    if odds_stop_time - os.time() > 0 then
        packet.room_info.odds_stop_time = odds_stop_time - os.time()
    end
    packet.flag = 0
    if flag ~= nil then
        packet.flag = 1
    end
    send_data(uid, packet)
    notice_arr[uid] = {}
end

function send_uid_odds_msg_old(uid)
    --    for i, v in pairs(admin_msg_old_old) do
    --        if v.cmd == 3004 and v.set == 4 then
    --            v.p1 = 0
    --            v.p2 = 0
    --            v.p3 = 0
    --            v.p4 = 0
    --            v.p5 = 0
    --            v.p6 = 0
    --        end
    --        if v.cmd == 3004 and v.set == 6 then
    --            v.pai = 0
    --        end
    --        if v.cmd == 3001 then
    --            v.notice = {}
    --        end
    --        if v.room_status ~= nil then
    --            v.room_status = nil
    --        end
    --        if v.only_uid ~= nil and v.only_uid == uid then
    --            send_data(uid, v, 1)
    --        end
    --        if v.only_uid == nil then
    --            send_data(uid, v, 1)
    --        end
    --    end

    for i, v in pairs(admin_msg_old) do
        if v.room_info ~= nil then
            local odds_stop_time_new = 0
            if odds_stop_time > 0 then
                odds_stop_time_new = odds_stop_time - os.time()
            end
            local odds_begin_time_cur = 0
            if odds_begin_time_switch == 1 then
                odds_begin_time_cur = os.time() - odds_begin_time
            end
            v.room_info = {
                room_id = v.room_info.room_id,
                boots_number = v.room_info.boots_number,
                ju = v.room_info.ju,
                odds_stop_time = odds_stop_time_new,
                odds_begin_time = odds_begin_time_cur,
            }
        end

        if v.only_uid ~= nil and v.only_uid == uid then
            send_data(uid, v, 1)
        end
        if v.only_uid == nil then
            send_data(uid, v, 1)
        end
        if v.cmd == 3001 then
            v.notice = {}
        end
    end
end


--推送历史消息
function send_admin_msg_old(uid)
    for i, v in pairs(admin_msg_old) do
        if v.room_info ~= nil then
            local odds_stop_time_new = 0
            if odds_stop_time > 0 then
                odds_stop_time_new = odds_stop_time - os.time()
            end
            local odds_begin_time_cur = 0
            if odds_begin_time_switch == 1 then
                odds_begin_time_cur = os.time() - odds_begin_time
            end
            v.room_info = {
                room_id = v.room_info.room_id,
                boots_number = v.room_info.boots_number,
                ju = v.room_info.ju,
                odds_stop_time = odds_stop_time_new,
                odds_begin_time = odds_begin_time_cur,
            }
        end
        if v.only_uid ~= nil and v.only_uid == uid then
            send_data(uid, v, 1)
        end
        if v.only_uid == nil then
            send_data(uid, v, 1)
        end
        if v.cmd == 3001 then
            v.notice = {}
        end
    end
end

function REQUEST.c2s_packet_get(uid, data)
    local packet_id = 0
    if data.packet_id ~= nil then
        packet_id = math.floor(tonumber(data.packet_id))
    end
    local packet = {
        cmd = NetCmd.S2C_PACKET_GET,
        ret = 3,
        packet_id = packet_id,
        money = PLAYR_OTHER[uid].score,
        add_money = 0,
    }
    if packet_id > 0 then
        local query_sql = "select * from packet where state = 1 and id =" .. packet_id
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)

        if rss ~= false and rss ~= nil and rss.id ~= nil then
            if ree ~= nil and ree.draw_num >= ree.num then
                packet.ret = 1
                send_data(uid, packet, 1)
                return
            end
            local query_sql = "select id from packet_list where uid =" .. uid .. " and packet_id=" .. packet_id
            local ree = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)
            if ree ~= false and ree ~= nil and ree.id ~= nil then
                packet.ret = 2
                send_data(uid, packet, 1)
                return
            end
            local add_money = 0
            if rss.num - rss.draw_num > 1 then
                if rss.type == 1 then
                    --固定金额
                    add_money = rss.money / rss.num
                else
                    --随机金额
                    local money_one = (rss.money - rss.draw_money) / rss.num
                    add_money = math.floor(money_one * math.random(50, 190)) / 100
                end
            else
                add_money = rss.money - rss.draw_money
            end
            local query_sql = 'begin;' --数据库事务开始
            local res = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

            local query_sql = "update packet set draw_num = draw_num + 1,draw_money = draw_money + " .. add_money
            if rss.num - rss.draw_num == 1 then
                query_sql = query_sql .. ", state = 2 "
            end
            query_sql = query_sql .. " where state = 1 and id =" .. packet_id
            local res = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

            if res and res.errno then
                local query_sql = 'rollback;' --数据库事务回退
                local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                packet.msg = '数据库异常,请重新领取红包'
                packet.errmsg = '数据库异常,请重新领取红包'
                packet.ret = 4
                send_data(uid, packet, 1)
                return
            else
                local query_sql = string.format("insert into `packet_list` (id, packet_id,uid,money,date) values( 0, %d, %d, %f,  '%s' )", packet_id, uid, add_money, os.date("%Y-%m-%d %H:%M:%S"))
                local res = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

                local update_sql = string.format("update user set score =score + %f where uid = %d", add_money, uid)
                local res = skynet.call("MG_SAVE", "lua", "update_sql", update_sql)
                if res and res.errno then
                    local query_sql = 'rollback;' --数据库事务回退
                    local res = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                    packet.msg = '数据库异常,请重新领取红包'
                    packet.errmsg = '数据库异常,请重新领取红包'
                    packet.ret = 4
                    send_data(uid, packet, 1)
                    return
                else
                    PLAYR_OTHER[uid].score = PLAYR_OTHER[uid].score + add_money
                    local db_data = {}
                    db_data.uid = uid
                    db_data.card_game_id = 0
                    db_data.time = os.date("%Y-%m-%d %H:%M:%S")
                    db_data.text = ''
                    db_data.type = 122 --领取红包
                    db_data.odds = add_money
                    db_data.score = PLAYR_OTHER[uid].score
                    db_data.rate = 1
                    db_data.score_change = add_money
                    db_data.score_after = add_money + PLAYR_OTHER[uid].score
                    db_data.user_ai = PLAYR_OTHER[uid].ai
                    db_data.tourist = PLAYR_OTHER[uid].tourist
                    db_data.orderid = packet_id
                    db_data.ukey = packet_id .. '_122'
                    local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
                    if user_score_log_id == 0 then
                        local query_sql = 'rollback;' --数据库事务回退
                        local res = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                        packet.msg = '数据库异常,请重新领取红包'
                        packet.errmsg = '数据库异常,请重新领取红包'
                        packet.ret = 4
                        send_data(uid, packet, 1)
                        return
                    end
                end
            end
            local query_sql = 'commit;' --数据库事务执行
            local res = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.add_money = add_money
            packet.money = PLAYR_OTHER[uid].score

            local query_sql = "select * from packet where id =" .. packet_id
            local rss = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)

            if rss ~= false and rss ~= nil and rss.id ~= nil then
                local query_sql = "select a.*,b.name,b.head from packet_list a left join user b on b.uid = a.uid where a.packet_id =" .. rss.id
                local ree = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
                local packet_list = {}

                if ree ~= false and ree ~= nil then
                    local kyy = 1
                    local max_moeny = 0
                    local max_key = 0
                    for i, v in pairs(ree) do
                        local arr = {}
                        arr.name = v.name
                        arr.date = v.date
                        arr.money = v.money
                        arr.head = v.head
                        arr.max_moeny = 0
                        if v.money > max_moeny then
                            max_moeny = v.money
                            max_key = kyy
                        end
                        packet_list[kyy] = clone(arr)
                        kyy = kyy + 1
                    end
                    packet_list[max_key].max_moeny = 1
                end
                packet.ret = 0
                packet.packet_list = packet_list
                packet.packet_info = { type = rss.type, num = rss.num, money = rss.money, draw_money = rss.draw_money, draw_num = rss.draw_num, date = rss.date }

                send_data(uid, packet, 1)
                return
            end
        end
    end
    packet.ret = 3
    send_data(uid, packet, 1)
end

function REQUEST.c2s_packet_list(uid, data)
    local packet_id = 0
    if data.packet_id ~= nil then
        packet_id = math.floor(tonumber(data.packet_id))
    end
    local packet = {
        cmd = NetCmd.S2C_PACKET_LIST,
        ret = 0,
        packet_id = packet_id,
    }
    if packet_id > 0 then
        local query_sql = "select * from packet where id =" .. packet_id
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)

        if rss ~= false and rss ~= nil and rss.id ~= nil then
            local query_sql = "select a.*,b.name,b.head from packet_list a left join user b on b.uid = a.uid where a.packet_id =" .. rss.id
            local ree = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
            local packet_list = {}

            if ree ~= false and ree ~= nil then
                local kyy = 1
                local max_moeny = 0
                local max_key = 0
                for i, v in pairs(ree) do
                    local arr = {}
                    arr.name = v.name
                    arr.date = v.date
                    arr.money = v.money
                    arr.head = v.head
                    arr.max_moeny = 0
                    if v.money > max_moeny then
                        max_moeny = v.money
                        max_key = kyy
                    end
                    packet_list[kyy] = clone(arr)
                    kyy = kyy + 1
                end
                packet_list[max_key].max_moeny = 1
            end
            packet.ret = 0
            packet.packet_list = packet_list
            packet.packet_info = { type = rss.type, num = rss.num, money = rss.money, draw_money = rss.draw_money, draw_num = rss.draw_num, date = rss.date }
            send_data(uid, packet, 1)
            return
        end
    end
    packet.ret = 1
    send_data(uid, packet, 1)
end

--发红包
function CMD.packet(uid)
    local packet = {
        cmd = NetCmd.S2C_PACKET_NEW,
        ret = 0,
        packet_id = 0,
        is_draw = 0,
    }
    if uid ~= nil and uid > 0 then
        local query_sql = "select id from packet where state = 1 "
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)

        if rss ~= nil and rss.id ~= nil then
            local query_sql = "select packet_id from packet_list where uid =" .. uid .. " and packet_id=" .. rss.id

            local ree = skynet.call("MG_SAVE", "lua", "mysql_get_one", query_sql)
            packet.packet_id = rss.id
            packet.uid = uid
            if ree == false or ree == nil or ree.packet_id == nil then
                packet.is_draw = 1
            end
            send_data(uid, packet, 1)
            return
        end
    else
        local query_sql = "select id from packet where state = 1 "
        local rss = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)

        if rss ~= nil and rss.id ~= nil then
            local query_sql = "select uid from packet_list where packet_id =" .. rss.id
            local ree = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
            local packet_is = {}
            if ree ~= nil then
                for i, v in pairs(ree) do
                    local uid = v.uid
                    packet_is[uid] = 1
                end
            end
            for uid, v in pairs(PLAYR_OTHER) do
                if PLAYR_OTHER[uid].ai == 0 and packet_is[uid] == nil then
                    packet.packet_id = rss.id
                    packet.uid = uid
                    send_data(uid, packet, 1)
                end
            end
        end
    end
end

function CMD.join(address, client_fd, uid, name, head, phone_num, money, score, no_say, ai, tourist)
    local query_sql = string.format("select *  from admin where uid=%d", uid)
    local admin_data = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if admin_data ~= nil and admin_data[1] ~= nil then
        if uid_is_admin[uid] == nil then
            uid_is_admin[uid] = uid
        end
        if PLAYR_OTHER[admin_uid] == nil then
            local query_sql = string.format("select * from user where uid=%d", admin_uid)
            local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            PLAYR_OTHER[admin_uid] = {
                uid = admin_uid,
                name = user_info[1].name,
                head = user_info[1].head,
                score = user_info[1].score,
                score_old = user_info[1].score,
                no_say = user_info[1].no_say,
                ai = user_info[1].ai,
                tourist = user_info[1].tourist
            }
        end
    end

    local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
    if score_log ~= nil and score_log[1] ~= nil then
        if score_log[1]['score_after'] ~= score then
            local db_data = {}
            db_data.uid = uid
            db_data.score = score
            db_data.score_log = score_log[1]['score_after']
            db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
            skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
            local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
            local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        end
    end

    if PLAYR_OTHER[uid] == nil then
        room_data.num = room_data.num + 1
        --用户数据初始化
        PLAYR_OTHER[uid] = {
            socket = client_fd,
            addr = address,
            uid = uid,
            name = name,
            head = head,
            phone_num = phone_num,
            money = money,
            score = score,
            score_old = score,
            no_say = no_say,
            ai = ai,
            tourist = tourist
        }
    else
        PLAYR_OTHER[uid].head = head
        PLAYR_OTHER[uid].name = name
        PLAYR_OTHER[uid].socket = client_fd
        PLAYR_OTHER[uid].addr = address
    end
    --公告 notice
    local query_sql = "select * from notice  where id not in (select notice_id from notice_uid where uid =" .. uid .. ") ORDER BY id DESC limit  0,1"
    local rss = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
    notice_arr[uid] = {}
    if rss ~= false then
        for i, v in pairs(rss) do
            local arr = {}
            arr.id = v.id
            arr.title = v.title
            arr.content = v.content
            arr.date = v.date
            table.insert(notice_arr[uid], arr)
            local query_sql = string.format("insert into `notice_uid` (uid, notice_id) values( %d, %d )", uid, v.id)
            skynet.call("MG_SAVE", "lua", "mysql_insert", query_sql)
        end
    end

    send_user_info(uid, PLAYR_OTHER[uid])
    CMD.packet(uid)
    send_data_to_admin(admin_uid, { cmd = 1001, room_num = room_data.num })

    -- if room_status == 1 then
    --     odss_log(uid)
    -- end
    if uid == admin_uid then
        REQUEST.set(admin_uid, { set = 32 }) --推送当局报表
        REQUEST.set(admin_uid, { set = 33 }) --推送自营报表
    end
    send_admin_msg_old(uid)
    --    if uid == admin_uid then
    --        send_admin_msg_old(admin_uid)
    --    else
    --        local time_out_stop_room_fun_agent = function()
    --            -- 定时器触发
    --            send_uid_odds_msg_old(uid)
    --        end
    --        add_skynet_timer(3, time_out_stop_room_fun_agent)
    --    end
end

function CMD.start(address)
    --获取系统配置
    room_data = skynet.call("MG_SAVE", "lua", "get_system")
    --获取牌局信息
    local game_info = skynet.call("MG_SAVE", "lua", "get_card_game")
    if game_info.state ~= 0 then
        game_info.ju = game_info.ju + 1
    end
    --获取路单数据
    local query_sql = "select * from card_game where zhuang > 0 and state != 2 and deleted=0 and id > (select id from card_game where start = 1 order by id desc limit 1) order by id asc"
    local rss = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
    ludan = {}
    ludan_card_game_id = {}
    if rss ~= false then
        for i, v in pairs(rss) do
            local arr = {}
            arr.y = v.zhuang
            arr.zd = v.zhuang_dui
            arr.xd = v.xian_dui
            ludan_card_game_id[v.id] = true
            table.insert(ludan, arr)
        end
    end

    card_game_id = game_info.id
    room_data.room_id = game_info.room_id
    room_data.boots_number = game_info.boots_number
    room_data.ju = game_info.ju

    room_data.ai_time_min = 0
    room_data.ai_time_max = 0
    if room_data.ai_time ~= nil then
        local timee = split(room_data.ai_time, ',')
        if timee[1] ~= nil then
            room_data.ai_time_min = math.floor(tonumber(timee[1]))
        end
        if timee[2] ~= nil then
            room_data.ai_time_max = math.floor(tonumber(timee[2]))
        end
        if room_data.ai_time_max < room_data.ai_time_min then
            room_data.ai_time_max = room_data.ai_time_min
        end
    end
    ludan_da_update()
end

--发送用户余额
function REQUEST.c2s_bjl_user_money(uid)
    local query_sql = string.format("SELECT  score FROM user WHERE uid=%d", uid)
    local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if user_info ~= nil and user_info[1] ~= nil then
        local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
        if score_log ~= nil and score_log[1] ~= nil then
            if score_log[1]['score_after'] ~= user_info[1]['score'] and PLAYR_OTHER[uid].tourist == 0 and PLAYR_OTHER[uid].ai == 0 then
                local db_data = {}
                db_data.uid = uid
                db_data.score = user_info[1]['score']
                db_data.score_log = score_log[1]['score_after']
                db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
                skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
                local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
                local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            end
            user_info[1]['score'] = score_log[1]['score_after']
        end
        local packet = {
            cmd = NetCmd.S2C_BJL_USER_MONEY,
            uid = uid,
            ret = 0,
            score = string.format('%.2f', user_info[1].score),
        }
        send_data(uid, packet)
    end
end


--申请下分 {"cmd":3025,"money":100,"channel":"11"}
function REQUEST.c2s_bjl_xia_fen(uid, data)
    uid = tonumber(uid)
    local cur_time = os.date("%Y-%m-%d %H:%M:%S")
    local packet = {
        cmd = NetCmd.S2C_BJL_XIA_FEN,
        uid = uid,
        ret = 1,
        time = cur_time,
    }
    if data.money == nil then
        data.money = 0
    else
        data.money = math.abs(data.money)
    end


    if data.money_type and data.yh_name and data.yh_hao then
        local query_sql = 'begin;' --数据库事务开始
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        --获取用户余额
        local user_score = 0
        local query_sql = string.format("SELECT  * FROM user WHERE uid=%d", uid)
        local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if user_info ~= nil and user_info[1] ~= nil then
            local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
            if score_log ~= nil and score_log[1] ~= nil then
                if score_log[1]['score_after'] ~= user_info[1]['score'] and PLAYR_OTHER[uid].tourist == 0 and PLAYR_OTHER[uid].ai == 0 then
                    local db_data = {}
                    db_data.uid = uid
                    db_data.score = user_info[1]['score']
                    db_data.score_log = score_log[1]['score_after']
                    db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
                    skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
                    local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
                    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                end
                user_info[1]['score'] = score_log[1]['score_after']
            end
            user_score = user_info[1]['score']
        else
            local query_sql = 'rollback;' --数据库事务回退
            skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '用户不存在'
            packet.errmsg = '用户不存在'
            send_data(uid, packet)
            return
        end

        if user_score < data.money then
            local query_sql = 'rollback;' --数据库事务回退
            skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '余额不足'
            packet.errmsg = '余额不足'
            send_data(uid, packet)
            return
        end

        local update_sql = string.format("update user set score =score - %f where uid = %d", data.money, uid)
        local res = skynet.call("MG_SAVE", "lua", "update_sql", update_sql)
        if res and res.errno then
            local query_sql = 'rollback;' --数据库事务回退
            skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '数据库异常,请重新下注'
            packet.errmsg = '数据库异常,请重新下注'
            packet.ret = 4
            send_data(uid, packet)
            return
        else
            local db_data = {}
            db_data.uid = uid
            db_data.time = cur_time
            db_data.type = 12
            db_data.money = user_score - data.money
            db_data.money_change = -data.money
            db_data.money_type = data.money_type
            db_data.yh_name = data.yh_name
            db_data.yh_hao = data.yh_hao
            db_data.text = ''
            db_data.user_ai = user_info[1].ai;
            db_data.tourist = user_info[1].tourist;
            db_data.card_game_id = card_game_id
            db_data.mktime = cur_time
            if data.yh ~= nil and data.yh ~= '' then
                db_data.text = json.encode({ yh = data.yh })
            end
            local user_money_log_id = skynet.call("MG_SAVE", "lua", "inser_user_money_log", db_data)
            if user_money_log_id == 0 then
                local query_sql = 'rollback;' --数据库事务回退
                skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                packet.msg = '网络异常，请重新申请'
                packet.errmsg = '网络异常，请重新申请'
                packet.ret = 4
                send_data(uid, packet)
                return
            end

            db_data.score = user_score
            db_data.score_change = -data.money
            db_data.score_after = user_score - data.money
            db_data.orderid = user_money_log_id
            db_data.ukey = user_money_log_id .. '_12'
            local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
            if user_score_log_id == 0 then
                local query_sql = 'rollback;' --数据库事务回退
                skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                packet.msg = '网络异常，请重新申请'
                packet.errmsg = '网络异常，请重新申请'
                packet.ret = 4
                send_data(uid, packet)
                return
            end
        end

        local query_sql = string.format("SELECT  score FROM user WHERE uid=%d", uid)
        local user_info_new = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if user_info_new[1]['score'] < 0 then
            local query_sql = 'rollback;' --数据库事务回退
            skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            packet.msg = '网络异常，请重新申请'
            packet.errmsg = '网络异常，请重新申请'
            packet.ret = 4
            send_data(uid, packet)
            return
        end
        local query_sql = 'commit;' --数据库事务执行
        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

        if PLAYR_OTHER[uid] == nil then
            PLAYR_OTHER[uid] = {
                uid = user_info[1].uid,
                name = user_info[1].name,
                head = user_info[1].head,
                phone_num = user_info[1].phone_num,
                money = user_info[1].money,
                score = user_info[1].score,
                score_old = user_info[1].score,
                ai = user_info[1].ai,
                no_say = user_info[1].no_say,
                tourist = user_info[1].tourist,
                odds_text = user_info[1].odds_text
            }
        end
        REQUEST.c2s_bjl_user_money(uid)
        PLAYR_OTHER[uid].score = user_score - data.money
        --初始分变化
        if PLAYR_OTHER[uid].score_old ~= nil then
            PLAYR_OTHER[uid].score_old = PLAYR_OTHER[uid].score_old - data.money
        else
            PLAYR_OTHER[uid].score_old = PLAYR_OTHER[uid].score
        end

        REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
        local web_domain = skynet.call('MG_SAVE', 'lua', 'get_web_domain');
        local packet = {
            cmd = NetCmd.S2C_BJL_XIA_FEN,
            uid = uid,
            ret = 0,
            msg = '下分' .. data.money,
            name = user_info[1].name,
            head = user_info[1].head,
            time = cur_time,
            color = 1,
            url = string.format('http://%s/server.php?db_name=%s', web_domain['domain'], skynet.getenv("database")),
            cur_time = os.date("%m-%d %H:%M:%S"),
        }
        send_data(uid, packet)

        local packet = {
            cmd = NetCmd.S2C_BJL_ODDS,
            uid = uid,
            ret = 0,
            msg = '下分' .. data.money,
            name = user_info[1].name,
            head = user_info[1].head,
            time = cur_time,
            cur_time = os.date("%m-%d %H:%M:%S"),
            color = 1,
        }
        send_all(packet)

        local packet = {
            cmd = NetCmd.S2C_BJL_ODDS,
            msg = '@' .. user_info[1].name .. '收到下分申请，请扫码联系财务完成下分。',
            ret = 0,
            name = PLAYR_OTHER[admin_uid].name,
            head = PLAYR_OTHER[admin_uid].head,
            time = cur_time,
            color = 2,
            cur_time = os.date("%m-%d %H:%M:%S"),
        }
        packet.only_uid = uid
        send_data(uid, packet)
        send_data_to_admin(admin_uid, packet)
        local packet = {
            cmd = NetCmd.S2C_BJL_XIA_FEN,
            uid = uid,
            ret = 0,
            msg = '有用户申请下分',
        }
        send_data_to_admin(admin_uid, packet)
    end
end

--{"cmd":3025,"fen":33,"channel":"10"}
function REQUEST.c2s_bjl_upfen(uid, data)
    uid = tonumber(uid)
    local cur_time = os.date("%Y-%m-%d %H:%M:%S")
    local web_domain = skynet.call('MG_SAVE', 'lua', 'get_web_domain');
    local packet = {
        cmd = NetCmd.S2C_BJL_UPFEN,
        uid = uid,
        ret = 0,
        msg = '申请已提交，请联系客服上分',
        url = string.format("http://%s/pay.php?db_name=%s", web_domain['domain'], skynet.getenv("database")),
        cur_time = os.date("%m-%d %H:%M:%S"),
        time = cur_time
    }
    local fen = 0;
    if data.fen ~= nil and tonumber(data.fen) ~= nil then
        fen = math.abs(data.fen)
    end
    local channel = 0
    if data.channel ~= nil and tonumber(data.channel) ~= nil then
        channel = math.abs(data.channel)
    end
    if fen == 0 then
        packet.ret = 1
        packet.msg = '请输入有效金额'
        packet.errmsg = '请输入有效金额'
        send_data(uid, packet)
        return
    end

    --获取用户余额
    local user_score = 0
    local user_info = skynet.call("MG_SAVE", "lua", "getUserInfo", uid)
    if user_info ~= nil and user_info[1] ~= nil then
        local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
        if score_log ~= nil and score_log[1] ~= nil then
            if score_log[1]['score_after'] ~= user_info[1]['score'] and PLAYR_OTHER[uid].tourist == 0 and PLAYR_OTHER[uid].ai == 0 then
                local db_data = {}
                db_data.uid = uid
                db_data.score = user_info[1]['score']
                db_data.score_log = score_log[1]['score_after']
                db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
                skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
                local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
                local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            end
            user_info[1]['score'] = score_log[1]['score_after']
        end
        user_score = user_info[1]['score']
    else
        packet.ret = 1
        packet.msg = '用户不存在'
        packet.errmsg = '用户不存在'
        send_data(uid, packet)
        return
    end

    --60秒限制
    local upfen_log = skynet.call("MG_SAVE", "lua", "select_upfen_log", uid)
    if upfen_log ~= nil and upfen_log[1] ~= nil then
        local upfen_log_time = upfen_log[1]['time']
        local cur_time = os.time()
        if cur_time - upfen_log_time <= 60 then
            packet.ret = 1
            packet.msg = '操作频繁，请60秒后重试'
            packet.errmsg = '操作频繁，请60秒后重试'
            send_data(uid, packet)
            return
        end
    end

    local db_data = {}
    db_data.uid = uid
    db_data.time = cur_time
    db_data.type = 11
    db_data.money_change = fen
    db_data.money = user_score + fen
    db_data.money_type = channel --0是微信 1是支付宝
    db_data.state = 0
    db_data.user_ai = user_info[1].ai
    db_data.tourist = user_info[1].tourist
    db_data.mktime = cur_time
    local pay_orderid = skynet.call("MG_SAVE", "lua", "upfen", db_data)
    --    if channel == 1 then
    --        packet.url = string.format("http://%s/payauto.php?pay_orderid=%d&pay_amount=%d", web_domain['domain'], pay_orderid, fen)
    --    end
    send_data(uid, packet)

    local packet = {
        cmd = NetCmd.S2C_BJL_ODDS,
        uid = uid,
        msg = '上分' .. fen,
        name = user_info[1].name,
        head = user_info[1].head,
        time = cur_time,
        cur_time = os.date("%m-%d %H:%M:%S"),
        color = 1,
        ret = 0,
    }
    send_all(packet)
    if admin_uid ~= nil and PLAYR_OTHER[admin_uid] ~= nil then
        local packet = {
            cmd = NetCmd.S2C_BJL_ODDS,
            msg = '@' .. user_info[1].name .. '收到上分申请，请扫码联系财务完成上分。',
            ret = 0,
            name = PLAYR_OTHER[admin_uid].name,
            head = PLAYR_OTHER[admin_uid].head,
            time = cur_time,
            color = 2,
            cur_time = os.date("%m-%d %H:%M:%S"),
        }
        packet.only_uid = uid
        send_data(uid, packet)
        send_data_to_admin(admin_uid, packet)
        local packet = {
            cmd = NetCmd.S2C_BJL_UPFEN,
            uid = uid,
            ret = 0,
            msg = '有用户申请上分',
        }
        send_data_to_admin(admin_uid, packet)
    end
end

--客服地址
function REQUEST.c2s_bjl_server(uid, data)
    local web_domain = skynet.call('MG_SAVE', 'lua', 'get_web_domain');
    local packet = {
        cmd = NetCmd.S2C_BJL_SERVER,
        uid = uid,
        url = string.format('http://%s/server.php?db_name=%s', web_domain['domain'], skynet.getenv("database"))
    }
    send_data(uid, packet)
end

--上下分处理
function REQUEST.c2s_updow_fen(uid, data)
    local cur_time = os.date("%Y-%m-%d %H:%M:%S")
    local packet = {
        cmd = NetCmd.S2C_UPDOW_FEN,
        ret = 0,
        msg = '操作成功',
        time = cur_time
    }
    if data.money_id ~= nil and data.zt ~= nil then
        local query_sql = 'begin;' --数据库事务开始
        skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        local query_sql = string.format("select money_change,uid,type  from user_money_log where id=%d and state=0", data.money_id)
        local money_res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if money_res ~= nil and money_res[1] ~= nil then
            local money_change = 0
            local state = 0
            local uid = money_res[1].uid
            local query_sql = string.format("SELECT  score,ai,tourist,name  FROM user WHERE uid=%d", uid)
            local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            --确定操作
            if data.zt == 2 then
                state = 1
                --上分
                if money_res[1]['type'] == 11 and money_res[1]['money_change'] > 0 then
                    money_change = money_res[1]['money_change']
                    local db_data = {}
                    db_data.uid = uid
                    db_data.card_game_id = card_game_id
                    db_data.time = cur_time
                    db_data.type = 11 --上分
                    db_data.score = user_info[1].score
                    db_data.score_change = money_change
                    db_data.score_after = user_info[1].score + money_change
                    db_data.user_ai = user_info[1].ai
                    db_data.tourist = user_info[1].tourist
                    db_data.orderid = data.money_id
                    db_data.ukey = data.money_id .. '_11'
                    local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
                    if user_score_log_id > 0 then
                        local query_sql = string.format("update user set score = score + %f where uid = %d", money_change, uid)
                        local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                        if res and res.errno then
                            local query_sql = 'rollback;' --数据库事务回退
                            local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                            return 0
                        end

                        REQUEST.c2s_bjl_user_money(uid)
                        local msg = '@' .. user_info[1].name .. ' 成功上分' .. money_change
                        local packet = {
                            cmd = NetCmd.S2C_BJL_ODDS,
                            msg = msg,
                            ret = 0,
                            name = PLAYR_OTHER[admin_uid].name,
                            head = PLAYR_OTHER[admin_uid].head,
                            time = cur_time,
                            color = 2,
                            cur_time = os.date("%m-%d %H:%M:%S"),
                        }
                        send_all(packet)
                    else
                        packet.ret = 1
                        packet.msg = '记录已处理'
                        send_data_to_admin(admin_uid, packet)
                        return
                    end
                end
                --下分
                if money_res[1]['type'] == 12 and money_res[1]['money_change'] < 0 then
                    if PLAYR_OTHER[uid].ai == 0 and PLAYR_OTHER[uid].tourist == 0 then
                        score_old_all = score_old_all + money_res[1]['money_change']
                    elseif PLAYR_OTHER[uid].tourist == 1 then
                        tourist_score_all = tourist_score_all + money_res[1]['money_change']
                    elseif PLAYR_OTHER[uid].ai == 1 then
                        ai_score_all = ai_score_all + money_res[1]['money_change']
                    end
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                    local msg = '@' .. user_info[1].name .. ' 成功下分' .. money_res[1]['money_change']
                    local packet = {
                        cmd = NetCmd.S2C_BJL_ODDS,
                        msg = msg,
                        ret = 0,
                        name = PLAYR_OTHER[admin_uid].name,
                        head = PLAYR_OTHER[admin_uid].head,
                        time = cur_time,
                        color = 2,
                        cur_time = os.date("%m-%d %H:%M:%S"),
                    }
                    send_all(packet)
                end
            end
            --删除操作
            if data.zt == 9 then
                state = 2
                --下分
                if money_res[1]['type'] == 12 and money_res[1]['money_change'] < 0 then
                    money_change = math.abs(money_res[1]['money_change'])
                    local db_data = {}
                    db_data.uid = uid
                    db_data.card_game_id = card_game_id
                    db_data.time = cur_time
                    db_data.type = 13 --下分删除
                    db_data.score = user_info[1].score
                    db_data.score_change = money_change
                    db_data.score_after = user_info[1].score + money_change
                    db_data.user_ai = user_info[1].ai
                    db_data.tourist = user_info[1].tourist
                    db_data.orderid = data.money_id
                    db_data.ukey = data.money_id .. '_13'
                    local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
                    if user_score_log_id > 0 then
                        local query_sql = string.format("update user set score = score + %f where uid = %d", money_change, uid)
                        local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                        if res and res.errno then
                            local query_sql = 'rollback;' --数据库事务回退
                            local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                            return 0
                        end

                        REQUEST.c2s_bjl_user_money(uid)
                        local msg = '@' .. user_info[1].name .. ' 成功上分' .. money_change
                        local packet = {
                            cmd = NetCmd.S2C_BJL_ODDS,
                            msg = msg,
                            ret = 0,
                            name = PLAYR_OTHER[admin_uid].name,
                            head = PLAYR_OTHER[admin_uid].head,
                            time = cur_time,
                            color = 2,
                            cur_time = os.date("%m-%d %H:%M:%S"),
                        }
                        send_all(packet)
                    else
                        packet.ret = 1
                        packet.msg = '记录已处理'
                        send_data_to_admin(admin_uid, packet)
                        return
                    end
                end
            end

            local query_sql = string.format("update user_money_log set state = %d where id = %d", state, data.money_id)
            local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
            if res and res.errno then
                local query_sql = 'rollback;' --数据库事务回退
                local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                return 0
            end
            local query_sql = 'commit;' --数据库事务执行
            skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)

            if data.zt == 9 then
                --后台删除下分记录，总分不变，个人积分增加
            else
                if user_info[1].ai == 0 and user_info[1].tourist == 0 and money_change ~= 0 then
                    score_old_all = score_old_all + money_change
                    score_all = score_all + money_change --总分变化
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                elseif user_info[1].ai == 0 and user_info[1].tourist == 1 then
                    if money_change > 0 then
                        tourist_score_all = tourist_score_all + money_change --总分变化
                        skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                    end
                elseif user_info[1].ai == 1 and money_change ~= 0 then
                    ai_score_all = ai_score_all + money_change --总分变化
                    skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
                end
            end

            if PLAYR_OTHER[uid] == nil then
                PLAYR_OTHER[uid] = {
                    uid = uid,
                    name = user_info[1].name,
                    head = user_info[1].head,
                    score = user_info[1].score + money_change,
                    score_old = user_info[1].score + money_change,
                    ai = user_info[1].ai,
                    tourist = user_info[1].tourist
                }
            else
                PLAYR_OTHER[uid].score_old = PLAYR_OTHER[uid].score_old + money_change
                PLAYR_OTHER[uid].score = user_info[1].score + money_change
            end
            REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
            send_data_to_admin(admin_uid, packet)
            return
        else
            packet.ret = 1
            packet.msg = '记录不存在或已处理'
            send_data_to_admin(admin_uid, packet)
            return
        end
    else
        packet.ret = 1
        packet.msg = '参数错误'
        send_data_to_admin(admin_uid, packet)
        return
    end
end

--手动上下分
function REQUEST.c2s_updow_fen_hand(uid, data)
    local cur_time = os.date("%Y-%m-%d %H:%M:%S")
    local packet = {
        cmd = NetCmd.S2C_UPDOW_FEN_HAND,
        ret = 0,
        msg = '操作成功',
        time = cur_time
    }
    if data.uid ~= nil and data.score ~= nil then
        local redis_setnx_key = skynet.getenv("port") .. '_odds_' .. uid
        local redis_result = skynet.call('MG_SAVE', 'lua', 'redis_setnx', redis_setnx_key, 1, 60)
        if redis_result == nil then
            packet.cmd = NetCmd.S2C_BJL_ODDS
            packet.msg = '@' .. PLAYR_OTHER[uid].name .. '您有注单正在处理，请稍后再试'
            packet.color = 2
            packet.ret = 0
            packet.onlu_uid = uid
            send_data(uid, packet)
            return
        end
        local uid = tonumber(data.uid)
        local money_change = tonumber(data.score)
        local query_sql = 'begin;' --数据库事务开始
        skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
        local query_sql = string.format("SELECT  score,ai,tourist,name  FROM user WHERE uid=%d", uid)
        local user_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if user_info ~= nil and user_info[1] ~= nil then
            local score_log = skynet.call("MG_SAVE", "lua", "select_user_score_log_last", uid)
            if score_log ~= nil and score_log[1] ~= nil then
                if score_log[1]['score_after'] ~= user_info[1].score and PLAYR_OTHER[uid].tourist == 0 and PLAYR_OTHER[uid].ai == 0 then
                    local db_data = {}
                    db_data.uid = uid
                    db_data.score = user_info[1].score
                    db_data.score_log = score_log[1]['score_after']
                    db_data.mktime = os.date("%Y-%m-%d %H:%M:%S")
                    skynet.call("MG_SAVE", "lua", "inser_score_error", db_data)
                    local query_sql = string.format("UPDATE user SET score= %.2f where uid = %d", score_log[1]['score_after'], uid)
                    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                end
                user_info[1].score = score_log[1]['score_after']
            end
            if money_change < 0 and user_info[1].score + money_change < 0 then
                packet.ret = 1
                packet.msg = '用户余分不足'
                send_data_to_admin(admin_uid, packet)
                --解锁
                skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                return
            end
            local type = 0
            local score_log_type = 0
            if money_change < 0 then
                type = 12 --下分
                score_log_type = 21
            elseif money_change > 0 then
                type = 11 --上分
                score_log_type = 20
            end
            local db_data = {}
            db_data.uid = uid
            db_data.card_game_id = card_game_id
            db_data.time = cur_time
            db_data.type = type
            db_data.score = user_info[1].score
            db_data.score_change = money_change
            db_data.score_after = user_info[1].score + money_change
            db_data.user_ai = user_info[1].ai
            db_data.tourist = user_info[1].tourist
            db_data.money = user_info[1].score + money_change
            db_data.money_change = money_change
            db_data.money_type = 0
            db_data.state = 0
            db_data.yh_name = ''
            db_data.yh_hao = ''
            db_data.text = ''
            db_data.mktime = cur_time
            local user_money_log_id = skynet.call("MG_SAVE", "lua", "inser_user_money_log", db_data)
            if user_money_log_id > 0 then
                db_data.type = score_log_type
                db_data.orderid = user_money_log_id
                db_data.ukey = user_money_log_id .. '_' .. score_log_type
                local user_score_log_id = skynet.call("MG_SAVE", "lua", "inser_user_score_log", db_data)
                if user_score_log_id > 0 then
                    local query_sql = string.format("update user_money_log set state = %d where id = %d", 1, user_money_log_id)
                    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                    if res and res.errno then
                        local query_sql = 'rollback;' --数据库事务回退
                        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                        --解锁
                        skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                        return 0
                    end
                    local query_sql = string.format("update user set score = score + %f where uid = %d", money_change, uid)
                    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
                    if res and res.errno then
                        local query_sql = 'rollback;' --数据库事务回退
                        local rss = skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                        --解锁
                        skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                        return 0
                    end
                else
                    local query_sql = 'rollback;' --数据库事务回退
                    skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                    packet.msg = '网络异常，请重新提交'
                    packet.ret = 1
                    send_data_to_admin(admin_uid, packet)
                    --解锁
                    skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                    return
                end
            else
                local query_sql = 'rollback;' --数据库事务回退
                skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
                packet.msg = '网络异常，请重新提交'
                packet.ret = 1
                send_data_to_admin(admin_uid, packet)
                --解锁
                skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
                return
            end
            local query_sql = 'commit;' --数据库事务执行
            skynet.call("MG_SAVE", "lua", "mysql_update", query_sql)
            if user_info[1].ai == 0 and user_info[1].tourist == 0 and money_change ~= 0 then
                score_old_all = score_old_all + money_change
                score_all = score_all + money_change --总分变化
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            elseif user_info[1].ai == 0 and user_info[1].tourist == 1 then
                tourist_score_all = tourist_score_all + money_change --总分变化
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            elseif user_info[1].ai == 1 and money_change ~= 0 then
                ai_score_all = ai_score_all + money_change --总分变化
                skynet.call("MG_SAVE", "lua", "insert_score_all", score_all, score_old_all, ai_score_all, tourist_score_all, 0, card_game_id)
            end
            if PLAYR_OTHER[uid] == nil then
                PLAYR_OTHER[uid] = {
                    uid = uid,
                    name = user_info[1].name,
                    head = user_info[1].head,
                    score = user_info[1].score + money_change,
                    score_old = user_info[1].score + money_change,
                    ai = user_info[1].ai,
                    tourist = user_info[1].tourist
                }
            else
                PLAYR_OTHER[uid].score_old = PLAYR_OTHER[uid].score_old + money_change
                PLAYR_OTHER[uid].score = user_info[1].score + money_change
            end

            local msg = '@' .. user_info[1].name
            if money_change > 0 then
                msg = msg .. ' 成功上分' .. money_change
            else
                msg = msg .. ' 成功下分' .. money_change
            end
            local packet = {
                cmd = NetCmd.S2C_BJL_ODDS,
                msg = msg,
                ret = 0,
                name = PLAYR_OTHER[admin_uid].name,
                head = PLAYR_OTHER[admin_uid].head,
                time = cur_time,
                color = 2,
                cur_time = os.date("%m-%d %H:%M:%S"),
            }
            send_all(packet)
            local packet = {
                cmd = NetCmd.S2C_UPDOW_FEN_HAND,
                money_change = money_change,
                ret = 0,
            }
            send_data_to_admin(admin_uid, packet)
            REQUEST.c2s_bjl_user_money(uid)
            REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        else
            packet.ret = 1
            packet.msg = '用户不存在'
            send_data_to_admin(admin_uid, packet)
            --解锁
            skynet.call('MG_SAVE', 'lua', 'redis_del', redis_setnx_key)
            return
        end
    else
        packet.ret = 1
        packet.msg = '参数错误'
        send_data_to_admin(admin_uid, packet)
        return
    end
end

--获取路单数据
function REQUEST.c2s_bjl_ludan(uid, data)

    local packet = {
        cmd = NetCmd.S2C_BJL_LUDAN,
        uid = uid,
        ret = 0,
        ludan_zz = ludan_new, --珠子路
        ludan_sx = ludan_sx, --三星路
        ludan_da = ludan_da_new, --大路
        ludan_dyz = ludan_dyz, --大眼仔路
        ludan_jy = ludan_jy, --曱甴路
        ludan_xiao = ludan_xiao, --小路规则
        ludan_yc = ludan_yc, --路单预测
        ludan_data = {
            ju = 0,
            z = 0,
            x = 0,
            h = 0,
            zd = 0,
            xd = 0
        }, --局数13 庄5 闲5 和3 庄对3 闲对3
    }

    --获取路单数据
    local query_sql = "select * from card_game where zhuang > 0 and state != 2 and deleted=0 and id > (select id from card_game where start = 1 order by id desc limit 1) order by id asc"
    local rss = skynet.call("MG_SAVE", "lua", "mysql_get_all", query_sql)
    ludan = {}
    ludan_card_game_id = {}
    if rss ~= false then
        for i, v in pairs(rss) do
            local arr = {}
            arr.y = v.zhuang
            arr.zd = v.zhuang_dui
            arr.xd = v.xian_dui
            ludan_card_game_id[v.id] = true
            table.insert(ludan, arr)
        end
    end
    ludan_da_update()
    packet.ludan_zz = ludan_new --珠子路
    packet.ludan_sx = ludan_sx --三星路
    packet.ludan_da = ludan_da_new --大路
    packet.ludan_dyz = ludan_dyz --大眼仔路
    packet.ludan_jy = ludan_jy --曱甴路
    packet.ludan_xiao = ludan_xiao --小路规则
    packet.ludan_yc = ludan_yc --路单预测
    packet.ludan_data = {
        ju = ludan_num_count[6],
        z = ludan_num_count[1],
        x = ludan_num_count[2],
        h = ludan_num_count[3],
        zd = ludan_num_count[4],
        xd = ludan_num_count[5]
    }

    send_data(uid, packet)
end

--机器人申请上下分
function REQUEST.c2s_ai_updow_fen(uid, data)
    local uid = tonumber(data.uid)
    --mark 1上分 2下分
    if data.mark == 1 then
        local ai_fen = tonumber(data.fen)
        if ai_fen ~= nil and ai_fen > 0 then
            ai_fen = math.floor(math.abs(ai_fen))
        else
            ai_fen = 0
        end
        if ai_fen > 0 then
            local data_rss = {}
            data_rss.fen = ai_fen
            data_rss.channel = 11
            REQUEST.c2s_bjl_upfen(uid, data_rss)
            return
        end
    elseif data.mark == 2 then
        local ai_fen = math.abs(data.fen)
        if ai_fen > 0 then
            local data_rss = {}
            data_rss.money = ai_fen
            data_rss.money_type = 1
            data_rss.yh_name = ''
            data_rss.yh_hao = ''
            REQUEST.c2s_bjl_xia_fen(uid, data_rss)
            return
        end
    end
end

--获取历史消息记录
function REQUEST.c2s_chat_history(uid, data)
    local packet = {
        cmd = NetCmd.S2C_CHAT_HISTORY,
        uid = uid,
        ret = 0,
        next_id = 0,
        data = {}
    }
    local id = data.id
    local query_sql = string.format("select chat_msg from chat_history where id=%d", id)
    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if res ~= nil and res[1] ~= nil then
        packet.data = json.decode(res[1]['chat_msg'])
    end
    local query_sql = string.format("select id from chat_history where id <%d order by id desc limit 1", id)
    local res = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
    if res ~= nil and res[1] ~= nil then
        packet.next_id = json.decode(res[1]['id'])
    end
    send_data_to_admin(admin_uid, packet)
    return
end

--修改机器人信息
function REQUEST.c2s_update_ai_info(uid, data)
    if data.robot_id ~= nil then
        local robot_id = math.floor(data.robot_id)
        local query_sql = string.format("select  uid,name,head,score,odds_text,upfen_text,auto_fen  from user where uid=%d", data.robot_id)
        local robot_info = skynet.call("MG_SAVE", "lua", "update_sql", query_sql)
        if PLAYR_OTHER[robot_id] ~= nil then
            PLAYR_OTHER[robot_id].name = robot_info[1]['name']
            PLAYR_OTHER[robot_id].head = robot_info[1]['head']
            PLAYR_OTHER[robot_id].score = robot_info[1]['score']
            PLAYR_OTHER[robot_id].odds_text = robot_info[1]['odds_text']
            PLAYR_OTHER[robot_id].upfen_text = robot_info[1]['upfen_text']
        else
            PLAYR_OTHER[robot_id] = {
                uid = robot_info[1].uid,
                name = robot_info[1].name,
                head = robot_info[1].head,
                score = robot_info[1].score,
                score_old = robot_info[1].score,
                tourist = 0,
                ai = 1,
                odds_text = robot_info[1].odds_text,
                auto_fen = robot_info[1].auto_fen,
                upfen_text = robot_info[1].upfen_text
            }
        end
        REQUEST.set(admin_uid, { set = 31 }) --更新会员列表
    end
end

REQUEST.func_map = {
    [NetCmd.C2S_BJL_SET] = REQUEST.set,
    [NetCmd.C2S_BJL_ODDS] = REQUEST.odds,
    [NetCmd.C2S_BJL_UP] = REQUEST.up,
    [NetCmd.C2S_BJL_LOG] = REQUEST.c2s_bjl_log,
    [NetCmd.C2S_BJL_ODDS_LOG] = REQUEST.c2s_bjl_odds_log,
    [NetCmd.C2S_BJL_AGENT] = REQUEST.c2s_bjl_agent,
    [NetCmd.C2S_BJL_XIA_FEN] = REQUEST.c2s_bjl_xia_fen,
    [NetCmd.C2S_BJL_ODDS_CANCEL] = REQUEST.c2s_bjl_odds_cancel,
    [NetCmd.C2S_BJL_USER_MONEY] = REQUEST.c2s_bjl_user_money,
    [NetCmd.C2S_BJL_UPFEN] = REQUEST.c2s_bjl_upfen,
    [NetCmd.C2S_BJL_SERVER] = REQUEST.c2s_bjl_server,
    [NetCmd.C2S_BJL_LUDAN] = REQUEST.c2s_bjl_ludan,
    [NetCmd.C2S_PACKET_GET] = REQUEST.c2s_packet_get,
    [NetCmd.C2S_PACKET_LIST] = REQUEST.c2s_packet_list,
    [NetCmd.C2S_AI_UPDOW_FEN] = REQUEST.c2s_ai_updow_fen,
    [NetCmd.C2S_CHAT_HISTORY] = REQUEST.c2s_chat_history,
    [NetCmd.C2S_UPDOW_FEN] = REQUEST.c2s_updow_fen,
    [NetCmd.C2S_UPDOW_FEN_HAND] = REQUEST.c2s_updow_fen_hand,
    [NetCmd.C2S_UPDATE_AI_INFO] = REQUEST.c2s_update_ai_info,
}

function find_id(address)
    for i, v in pairs(PLAYR_OTHER) do
        if v.addr ~= nil and v.addr == address then
            return i, i
        end
    end
    return nil, nil
end

skynet.register_protocol {
    name = "client",
    id = skynet.PTYPE_CLIENT,
    unpack = function(msg, sz)
        return skynet.tostring(msg, sz)
    end,
    dispatch = function(_, address, text, ...)
        local id, uid = find_id(address)
        if not id then
            skynet.error("invalid address", address)
            return
        end
        local json_data = unpack_package(text, room_id, uid)
        if not json_data then
            if not json_data then
                skynet.error "invalid json data"
            end
            return
        end
        skynet.error('==room.lua===json=====', uid, json.encode(json_data))
        local f = REQUEST.func_map[json_data.cmd]
        if not f then
            return
        end
        f(id, json_data)
    end
}

skynet.start(function()
    skynet.dispatch("lua", function(_, address, command, ...)
        local f = CMD[command]
        skynet.ret(skynet.pack(f(address, ...)))
    end)
end)
